码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

时间:2014-09-08 10:44:56      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   strong   for   

Untrusted Patrol

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 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. 

Input

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.

Output

For each test case, output "Yes" if the security man worked normally 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




转自:http://blog.csdn.net/napoleon_acm/article/details/39124615

题目: LINK

给定一个无向图,n个点,  m条边,k个特殊点(有传感器),只有当第一次到达特殊点的时候才会发出信号,给出发出信号的序列,问是否存在这样的路径使得每个点至少遍历一次,而且特殊点第一次到达的顺序和和题目输入一样。  (1 <= N <= 100000), M (1 <= M <= 200000)  先特判 如果询问时输入的L<k, 那么直接No, 因为l<k肯定有传感器的点没有到达,不满足每个点都遍历一次。 先把第一个特殊点入队,遍历所有的可以到达的点(中途不经过其他特殊点),标记为可以到达。  之后判断第二个点是否可以从第一个点到达(是否被标记),如果不可以则No,否则遍历从第二个特殊点出发的可以到达的点(同样中途不经过剩余特殊点). ....... 依次处理完所有点即可. 如果前面的一个特殊点可以到达某个点,那么 他后面的特殊点一定也可以到达这些点,因为他可以回到前面的特殊点再走过去.

 

本题可用多种思路做,但是拍代码前一定要先分析好再开拍,不然,,,

可用方法:bfs、dfs、并查集。

吐血ac。。。

 

代码:

bfs:

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 
 10 #define N 100005
 11 #define M 15
 12 #define mod 6
 13 #define mod2 100000000
 14 #define ll long long
 15 #define maxi(a,b) (a)>(b)? (a) : (b)
 16 #define mini(a,b) (a)<(b)? (a) : (b)
 17 
 18 using namespace std;
 19 
 20 int T;
 21 int n,m,k,l;
 22 int vis[N];
 23 int vis2[N];
 24 vector<int>bian[N];
 25 int re[N];
 26 int a,b;
 27 vector<int>::iterator it;
 28 
 29 void ini()
 30 {
 31     int i;
 32     memset(vis,0,sizeof(vis));
 33     memset(vis2,0,sizeof(vis2));
 34     for(i=1;i<=100003;i++){
 35         bian[i].clear();
 36     }
 37     scanf("%d%d%d",&n,&m,&k);
 38     for(i=1;i<=k;i++){
 39         scanf("%d",&re[i]);
 40     }
 41     while(m--){
 42         scanf("%d%d",&a,&b);
 43         bian[a].push_back(b);
 44         bian[b].push_back(a);
 45     }
 46     scanf("%d",&l);
 47     for(i=1;i<=l;i++){
 48         scanf("%d",&re[i]);
 49         vis2[ re[i] ]=1;
 50     }
 51 }
 52 
 53 int solve()
 54 {
 55     if(l!=k){
 56         return 0;
 57     }
 58     queue<int>q;
 59     int r;
 60     vis[ re[1] ]=1;
 61     for(int i=1;i<=l;i++){
 62        // printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]);
 63         if(vis[ re[i] ]==0) return 0;
 64        // vis2[ re[i] ]=0;
 65         q.push(re[i]);
 66         while( q.size()>0 )
 67         {
 68             r=q.front();
 69             //printf("  r=%d\n",r);
 70             q.pop();
 71            // vis[r]=1;
 72 
 73             for(it=bian[r].begin();it!=bian[r].end();it++){
 74              //   printf("   it=%d\n",*it);
 75                 if(vis[*it]==1) continue;
 76                 vis[ *it ]=1;
 77                 if( vis2[ *it ]==0 ){
 78                     q.push( (*it) );
 79                 }
 80             }
 81 /*
 82             for(int j=0;j<(int)bian[r].size();j++){
 83                 int y=bian[r][j];
 84                 if(vis[y]==1) continue;
 85                 vis[y]=1;
 86                 if(vis2[y]==0){
 87                     q.push(y);                }
 88             }*/
 89         }
 90     }
 91 
 92     for(int i=1;i<=n;i++){
 93         if(vis[i]==0) return 0;
 94     }
 95     return 1;
 96 }
 97 
 98 
 99 int main()
100 {
101     //freopen("data.in","r",stdin);
102     scanf("%d",&T);
103     for(int cnt=1;cnt<=T;cnt++)
104    // while(T--)
105     //while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
106     {
107         ini();
108         if(solve()==0){
109             printf("No\n");
110         }
111         else{
112             printf("Yes\n");
113         }
114     }
115 
116     return 0;
117 }

 

 

dfs版本:

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 
 10 #define N 100005
 11 #define M 15
 12 #define mod 6
 13 #define mod2 100000000
 14 #define ll long long
 15 #define maxi(a,b) (a)>(b)? (a) : (b)
 16 #define mini(a,b) (a)<(b)? (a) : (b)
 17 
 18 using namespace std;
 19 
 20 int T;
 21 int n,m,k,l;
 22 int vis[N];
 23 int vis2[N];
 24 vector<int>bian[N];
 25 int re[N];
 26 int a,b;
 27 
 28 void ini()
 29 {
 30     int i;
 31     memset(vis,0,sizeof(vis));
 32     memset(vis2,0,sizeof(vis2));
 33     for(i=1;i<=100003;i++){
 34         bian[i].clear();
 35     }
 36     scanf("%d%d%d",&n,&m,&k);
 37     for(i=1;i<=k;i++){
 38         scanf("%d",&re[i]);
 39     }
 40     while(m--){
 41         scanf("%d%d",&a,&b);
 42         bian[a].push_back(b);
 43         bian[b].push_back(a);
 44     }
 45     scanf("%d",&l);
 46     for(i=1;i<=l;i++){
 47         scanf("%d",&re[i]);
 48         vis2[ re[i] ]=1;
 49     }
 50 }
 51 
 52 void dfs(int s)
 53 {
 54     vector<int>::iterator it;
 55     for(it=bian[s].begin();it!=bian[s].end();it++){
 56         if(vis[*it]==1) continue;
 57         vis[*it]=1;
 58         if(vis2[*it]==0){
 59             dfs(*it);
 60         }
 61     }
 62 }
 63 
 64 int solve()
 65 {
 66     if(l!=k){
 67         return 0;
 68     }
 69     vis[ re[1] ]=1;
 70     for(int i=1;i<=l;i++){
 71        // printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]);
 72         if(vis[ re[i] ]==0) return 0;
 73         dfs(re[i]);
 74     }
 75 
 76     for(int i=1;i<=n;i++){
 77         if(vis[i]==0) return 0;
 78     }
 79     return 1;
 80 }
 81 
 82 
 83 int main()
 84 {
 85     //freopen("data.in","r",stdin);
 86     scanf("%d",&T);
 87     for(int cnt=1;cnt<=T;cnt++)
 88    // while(T--)
 89     //while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
 90     {
 91         ini();
 92         if(solve()==0){
 93             printf("No\n");
 94         }
 95         else{
 96             printf("Yes\n");
 97         }
 98     }
 99 
100     return 0;
101 }

 

ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

标签:style   blog   http   color   os   io   ar   strong   for   

原文地址:http://www.cnblogs.com/njczy2010/p/3961102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!