ZOJ-3811 Untrusted Patrol DFS 2014 Mudanjiang Network Competition C, zoj-3811untrusted
N points, m bidirectional edges, and k sensors. One sensor records the time sequence of the first arrival and determines whether all vertices can be checked.
First, Judge l. l <k must not work. Then, according to the sensor's time sequence dfs, first searches for dfs from the first sensor location and finds all the locations that reach the sensor. If the sensor is marked as accessible, search for the next sensor from this sensor. Otherwise, the time sequence cannot be met. Finally, we need to determine the connectivity of the entire graph. All vertices must be reachable.
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <iomanip>#include <cstdlib>#include <algorithm>using namespace std;const int maxn=110000;int head[maxn];int visit[maxn];int pile[maxn];int n,m,k,l;int num;int s;struct Edge{int u,v;int next;}edge[maxn*4];void addedge(int u,int v){edge[num].u=u;edge[num].v=v;edge[num].next=head[u];head[u]=num++;edge[num].u=v;edge[num].v=u;edge[num].next=head[v];head[v]=num++;}void dfs(int u){visit[u]=1;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].v;if(visit[v]){continue;}if(pile[v]){visit[v]=1;continue;}dfs(v);}}void dfs1(int u){visit[u]=1;s++;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].v;if(!visit[v]){dfs1(v);}}}int main(){int t;int flag;int u,v;scanf("%d",&t);while(t--){flag=1;s=0;num=0;memset(head,-1,sizeof(head));memset(visit,0,sizeof(visit));memset(pile,0,sizeof(pile));scanf("%d%d%d",&n,&m,&k);for(int i=0;i<k;i++){scanf("%d",&u);pile[u]=1;}for(int i=0;i<m;i++){scanf("%d%d",&u,&v);addedge(u,v);}scanf("%d",&l);if(l<k){flag=0;}scanf("%d",&u);dfs(u);for(int i=1;i<l;i++){scanf("%d",&u);if(!visit[u]){flag=0;}if(flag){dfs(u);}}memset(visit,0,sizeof(visit));dfs1(1);if(s<n){flag=0;}if(flag){printf("Yes\n");}else{printf("No\n");}}return 0;}