dfs 【链式前向星速度在此秒杀vector】

一道地图路径问题,给定一棵树结构的图,从多个起点出发到节点1,求途中消灭叛军的数量。采用深度优先搜索(DFS)策略,使用链式前向星比vector在本题中表现更优,应当谨慎选择数据结构。
摘要由CSDN通过智能技术生成


题意:

给你n个点的地图,这个地图由n-1条边链接,那肯定是树了,然后给你m个起点,问你从这m个起点出发到达1,路上消灭的叛军数量。

题解:

直接从1深搜,跑一边地图,回溯时,如果出现起点,那么之前的叛军全部消灭。


值得注意的是  vector的速度在这道题远远大于链式前向星。

以后慎用vector。。。



#include<string>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#define ll long long
#define x first
#define y second
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int mod=11;
const int maxn=1e5+100;
int n,m,cnt;
ll ans;
int cost[maxn];
bool vis[maxn],book[maxn];
int head[maxn];
struct node{
    int to,nxt;
}e[maxn*2];

void addedge(int u,int v){
    e[++
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值