标签:
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 the warehouse. The warehouse has N piles of drinks and M passageways connected them (warehouse is not big enough). When the evening comes, the security man will start to patrol the warehouse following a path to check all piles of drinks. Unfortunately, Edward is a suspicious man, so he sets sensors on K piles of the drinks. When the security man comes to check the drinks, the sensor will record a message. Because of the memory limit, the sensors can only record for the first time of the security man‘s visit. After a peaceful evening, Edward gathered all messages ordered by recording time. He wants to know whether is possible that the 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 is guaranteed that the sensors work properly. However, Edward thinks the security man may not works as expected. For example, he may digs through walls, climb over piles, use some black magic to teleport to anywhere and so on.
There are multiple test cases. The first line of input is an integer T indicates the 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 have sensors installed. The following M lines, each line contains two integers Ai and 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 next line contains L distinct integers. These are the indexes of piles where the messages came from (each is among the K integers above), ordered by recording time.
For each test case, output "Yes" if the security man worked normally and has checked all piles of drinks, or "No" if not.
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
No
Yes
Author: DAI, Longao
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round
据说这题是bfs裸题,事实也是如此。
这里用两种实现方法,dfs和bfs,实际上这两种方法差不多。
注意两种特判条件。
dfs
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<set> 6 #include<map> 7 using namespace std; 8 #define N 100006 9 int n,m,k; 10 vector<int> v[N]; 11 set<int> s; 12 int tag[N]; 13 int vis[N]; 14 int a[N]; 15 void dfs(int st){ 16 vis[st]=1; 17 for(int i=0;i<v[st].size();i++){ 18 int u=v[st][i]; 19 if(!vis[u]){ 20 if(tag[u]) s.insert(u); 21 else dfs(u); 22 } 23 } 24 } 25 int main() 26 { 27 int t; 28 scanf("%d",&t); 29 while(t--){ 30 31 for(int i=0;i<N;i++){ 32 v[i].clear(); 33 } 34 memset(tag,0,sizeof(tag)); 35 s.clear(); 36 memset(vis,0,sizeof(vis)); 37 38 scanf("%d%d%d",&n,&m,&k); 39 int x,y; 40 for(int i=0;i<k;i++)scanf("%d",&x); 41 for(int i=0;i<m;i++){ 42 scanf("%d%d",&x,&y); 43 v[x].push_back(y); 44 v[y].push_back(x); 45 } 46 int L; 47 scanf("%d",&L); 48 for(int i=0;i<L;i++){ 49 scanf("%d",&a[i]); 50 tag[a[i]]=1; 51 } 52 if(L<k){ 53 printf("No\n"); 54 continue; 55 } 56 dfs(a[0]); 57 58 int flag=1; 59 for(int i=1;i<L;i++){ 60 if(s.find(a[i])==s.end()){ 61 flag=0; 62 break; 63 } 64 else{ 65 s.erase(a[i]); 66 dfs(a[i]); 67 } 68 } 69 70 for(int i=1;i<=n;i++){ 71 if(!vis[i]){ 72 flag=0; 73 break; 74 } 75 } 76 if(flag==1){ 77 printf("Yes\n"); 78 }else{ 79 printf("No\n"); 80 } 81 82 } 83 return 0; 84 }
bfs
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<set> 6 #include<map> 7 #include<queue> 8 using namespace std; 9 #define N 100006 10 int n,m,k; 11 vector<int> v[N]; 12 set<int> s; 13 int tag[N]; 14 int vis[N]; 15 int a[N]; 16 void bfs(int st){ 17 queue<int>q; 18 q.push(st); 19 vis[st]=1; 20 while(!q.empty()){ 21 int tmp=q.front(); 22 q.pop(); 23 for(int i=0;i<v[tmp].size();i++){ 24 int u=v[tmp][i]; 25 if(!vis[u]){ 26 if(tag[u]) s.insert(u); 27 else q.push(u); 28 vis[u]=1; 29 } 30 } 31 } 32 } 33 int main() 34 { 35 int t; 36 scanf("%d",&t); 37 while(t--){ 38 39 for(int i=0;i<N;i++){ 40 v[i].clear(); 41 } 42 memset(tag,0,sizeof(tag)); 43 s.clear(); 44 memset(vis,0,sizeof(vis)); 45 46 scanf("%d%d%d",&n,&m,&k); 47 int x,y; 48 for(int i=0;i<k;i++)scanf("%d",&x); 49 for(int i=0;i<m;i++){ 50 scanf("%d%d",&x,&y); 51 v[x].push_back(y); 52 v[y].push_back(x); 53 } 54 int L; 55 scanf("%d",&L); 56 for(int i=0;i<L;i++){ 57 scanf("%d",&a[i]); 58 tag[a[i]]=1; 59 } 60 if(L<k){ 61 printf("No\n"); 62 continue; 63 } 64 bfs(a[0]); 65 66 int flag=1; 67 for(int i=1;i<L;i++){ 68 if(s.find(a[i])==s.end()){ 69 flag=0; 70 break; 71 } 72 else{ 73 s.erase(a[i]); 74 bfs(a[i]); 75 } 76 } 77 for(int i=1;i<=n;i++){ 78 if(!vis[i]){ 79 flag=0; 80 break; 81 } 82 } 83 if(flag) printf("Yes\n"); 84 else printf("No\n"); 85 } 86 return 0; 87 }
zoj 3811 Untrusted Patrol(bfs或dfs)
标签:
原文地址:http://www.cnblogs.com/UniqueColor/p/4769558.html