Time Limit: 3 Seconds Memory Limit: 65536
KB
Edward is a rich man. He owns a large factory for health drink production.As a matter of course, there is a large warehouse in the factory.
To ensure the safety of drinks, Edward hired a security man to patrol thewarehouse. The warehouse has N piles of drinks
and M passageways connected them (warehouse is not big enough). When the eveningcomes, the security man will start to patrol the warehouse following a path tocheck all piles of drinks.
Unfortunately, Edward is a suspicious man, so he sets sensors on K pilesof the drinks. When the security man comes
to check the drinks, the sensor willrecord a message. Because of the memory limit, the sensors can only record forthe first time of the security man‘s visit.
After a peaceful evening, Edward gathered all messages ordered byrecording time. He wants to know whether is possible thatthe
security man has checked all piles of drinks. Can you help him?
The security man may start to patrol at any piles of drinks. It isguaranteed that the sensors work properly. However, Edward thinks the securityman may not works as expected. For example, he
may digs through walls, climbover piles, use some black magic to teleport to anywhere and so on.
Input
There are multiple test cases. The first line of input is an integer T indicatesthe number of test cases. For each
test case:
The first line contains three integers N (1<= N <=
100000), M (1 <= M <= 200000) and K (1
<= K <= N).
The next line contains K distinct integers indicating the indexes of piles (1-based) that havesensors installed. The
following M lines, each line contains two integers Aiand Bi (1
<= Ai, Bi <= N)
which indicates a bidirectional passageway connects piles Ai and Bi.
Then, there is an integer L (1 <= L <= K)
indicating the number of messages gathered from all sensors. The nextline contains L distinct integers. These are the indexes of piles where the messages camefrom (each is among the K integers
above), ordered by recording time.
Output
For each test case, output "Yes" if the security man workednormally and has checked all piles of drinks, or "No" if not.
Sample Input
2
5 5 3
1 2 4
1 2
2 3
3 1
1 4
4 5
3
4 2 1
5 5 3
1 2 4
1 2
2 3
3 1
1 4
4 5
3
4 1 2
Sample Output
No
Yes
题意:n个点,m条双向边,k个传感器。其中有l个传感器记录到了第一次到达的时间顺序,求是否有可能检查了所有的顶点。
#include<stdio.h> #include<string.h> #define MAXN 100002 #define MAXM 400004 char vis[MAXN],ts[MAXN]; int next[MAXM],p[MAXM],q[MAXM]; int first[MAXN],K[MAXN]; int counts,cnt; void dfs(int u) { int e,v; e=first[u]; while(e!=0) { v=q[e]; if(vis[v]==0) { if(ts[v]==1) { cnt++; counts++; vis[v]=1; ts[v]=0; } else { cnt++; vis[v]=1; dfs(v); } } e=next[e]; } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif int e,i,T,n,m,k,x,u,v,l; scanf("%d",&T); while(T--) { memset(first,0,sizeof(first)); memset(ts,0,sizeof(ts)); memset(vis,0,sizeof(vis)); scanf("%d%d%d",&n,&m,&k); for(i=0;i<k;i++) // 对监控仪标记 { scanf("%d",&x); ts[x]=1; } for( e=1;e<=m;e++) // 输入通道 { scanf("%d%d",&u,&v); p[e+m]=q[e]=v; q[e+m]=p[e]=u; next[e]=first[u]; // 初始化链表 first[u]=e; next[e+m]=first[v]; first[v]=e+m; } scanf("%d",&l); for(i=0;i<l;i++)scanf("%d",&K[i]); // 搜集的数据 if(l<k)printf("No\n"); else { ts[K[0]]=0; vis[K[0]]=1; for(i=0,counts=1,cnt=1;i<l;i++) { if(ts[K[i]]==1)break; dfs(K[i]); } if(counts==l&&cnt==n) printf("Yes\n"); else printf("No\n"); } } return 0; }