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

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

 

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

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

吐血ac。。。

 

代码:

bfs:

  1 #include
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  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
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  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 }

 

 

并查集版:http://blog.csdn.net/qq574857122/article/details/39121391

转自:

思路:

先把无触发器的点放到图里。

然后根据触发器的信号顺序把点依次加入图中,加入时只添加(与无触发器点相连的边)

然后判断这个点能否与之前的图连通。bfs+并查集判断。

==其实并查集就够了,写的时候有脑洞,bfs就冒出来了

 

  1 #include
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  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 f[N];
 23 int rank[N];
 24 int vis2[N];
 25 vector<int>bian[N];
 26 int re[N];
 27 vector<int>::iterator it;
 28 
 29 int find(int v)
 30 {
 31     //printf("   v=%d f=%d\n",v,f[v]);
 32     return f[v]=f[v]==v ? v : find(f[v]);
 33 }
 34 
 35 void merge(int x,int y)
 36 {
 37     int a=find(x),b=find(y);
 38     if(a==b) return;
 39     if(rank[a]<rank[b]){
 40         f[a]=b;
 41     }
 42     else{
 43         f[b]=a;
 44         if(rank[b]==rank[a]){
 45             ++rank[a];
 46         }
 47     }
 48 }
 49 
 50 void ini()
 51 {
 52     int i;
 53     int a,b;
 54     //memset(vis,0,sizeof(vis));
 55     memset(rank,0,sizeof(rank));
 56     memset(vis2,0,sizeof(vis2));
 57     for(i=1;i<=100003;i++){
 58         bian[i].clear();
 59     }
 60 
 61     scanf("%d%d%d",&n,&m,&k);
 62     for(i=1;i<=n;i++){
 63         f[i]=i;
 64        // printf("   i=%d f=%d\n",i,f[i]);
 65     }
 66     for(i=1;i<=k;i++){
 67         scanf("%d",&re[i]);
 68     }
 69     while(m--){
 70         scanf("%d%d",&a,&b);
 71         bian[a].push_back(b);
 72         bian[b].push_back(a);
 73     }
 74     scanf("%d",&l);
 75     for(i=1;i<=l;i++){
 76         scanf("%d",&re[i]);
 77         vis2[ re[i] ]=1;
 78     }
 79 }
 80 
 81 int solve()
 82 {
 83     int i,x,y;
 84     if(l!=k){
 85         return 0;
 86     }
 87 
 88     // for(i=1;i<=n;i++){
 89     //    printf("  i=%d f=%d\n",i,f[i]);
 90    // }
 91    // vis[ re[1] ]=1;
 92     for(i=1;i<=n;i++){
 93         if(vis2[i]==1) continue;
 94         for(it=bian[i].begin();it!=bian[i].end();it++){
 95             if(vis2[*it]==1) continue;
 96             merge(i,*it);
 97         }
 98     }
 99 
100     i=1;
101     for(it=bian[ re[i] ].begin();it!=bian[  re[i] ].end();it++){
102         if(vis2[*it]==1) continue;
103         merge(re[i],*it);
104     }
105     vis2[ re[1] ]=0;
106 
107     for(i=2;i<=l;i++){
108        // printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]);
109         for(it=bian[ re[i] ].begin();it!=bian[  re[i] ].end();it++){
110             if(vis2[*it]==1) continue;
111             merge(re[i],*it);
112         }
113         x=find(re[i-1]);
114         y=find(re[i]);
115         //printf("  re[i-1]=%d re[i]=%d x=%d y=%d\n",re[i-1],re[i],x,y);
116         if(x!=y) return 0;
117         vis2[ re[i] ]=0;
118     }
119     x=find(1);
120     for(i=2;i<=n;i++){
121         if(x!=find(i)) return 0;
122     }
123     return 1;
124 }
125 
126 
127 int main()
128 {
129     //freopen("data.in","r",stdin);
130     scanf("%d",&T);
131     for(int cnt=1;cnt<=T;cnt++)
132    // while(T--)
133     //while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
134     {
135         ini();
136         if(solve()==0){
137             printf("No\n");
138         }
139         else{
140             printf("Yes\n");
141         }
142     }
143 
144     return 0;
145 }

 

转载于:https://www.cnblogs.com/njczy2010/p/3961102.html

你可能感兴趣的:(ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集)