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

CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)

时间:2015-12-04 22:50:10      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

Description

A thief is running away!
We can consider the city where he locates as an undirected graph in which nodes stand for crosses and edges stand for streets. The crosses are labeled from 0 to N–1. 
The tricky thief starts his escaping from cross S. Each moment he moves to an adjacent cross. More exactly, assume he is at cross u at the moment t. He may appear at cross v at moment t + 1 if and only if there is a street between cross u and cross v. Notice that he may not stay at the same cross in two consecutive moment.
The cops want to know if there’s some moment at which it’s possible for the thief to appear at any cross in the city.

 

Input

The input contains multiple test cases:
In the first line of the input there’s an integer T which is the number of test cases. Then the description of T test cases will be given. 
For any test case, the first line contains three integers N (≤ 100 000), M (≤ 500 000), and S. N is the number of crosses. M is the number of streets and S is the index of the cross where the thief starts his escaping.
For the next M lines, there will be 2 integers u and v in each line (0 ≤ u, v < N). It means there’s an undirected street between cross u and cross v.

 

Output

 

For each test case, output one line to tell if there’s a moment that it’s possible for the thief to appear at any cross. Look at the sample output for output format.

 

 

 

Sample Input

2
3 3 0
0 1
0 2
1 2
2 1 0
0 1

 

Sample Output

Case 1: YES
Case 2: NO

 

Hint

 

For the first case, just look at the table below. (YES means the thief may appear at the cross at that moment)

技术分享

For the second input, at any moment, there’s at least one cross that the thief can’t reach.

 

题意:给出一个起始点,一些边,有人从这个起始点开始随意走,问在某一个时候,它是否可以处于任意位置。

思路:思考下,就可以明白,只要是一个联通图,并且存在奇数点形成的环,那么在某一个时候就可以处于任意位置。

如何判断存在一个奇数点形成的环?

染色法:就是给每个点进行标号,标为0,1如果存在一条边连接的两个点标号相同,那么就是存在一个奇数环......

 

第一种写法,和hdu4751比较

技术分享
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 100006
23 #define inf 1e12
24 int n,m,s;
25 vector<int> g[N];
26 int color[N];
27 bool dfs(int u,int c){
28    color[u]=c;
29    for(int i=0;i<g[u].size();i++){
30       int next=g[u][i];
31       if(color[next]!=-1){
32          if(color[next]==c){
33             return true;
34          }
35          continue;
36       }
37       if(dfs(next,!c)) return true;
38    }
39    return false;
40 }
41 int main()
42 {
43    int t;
44    int ac=0;
45    scanf("%d",&t);
46    while(t--){
47       for(int i=0;i<N;i++){
48          g[i].clear();
49       }
50       scanf("%d%d%d",&n,&m,&s);
51       for(int i=0;i<m;i++){
52          int u,v;
53          scanf("%d%d",&u,&v);
54          g[u].push_back(v);
55          g[v].push_back(u);
56       }
57       memset(color,-1,sizeof(color));
58       printf("Case %d: ",++ac);
59       if(dfs(s,0)){
60          printf("YES\n");
61       }else{
62          printf("NO\n");
63       }
64    }
65     return 0;
66 }
View Code

 

第二种写法:

技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<queue>
 6 using namespace std;
 7 #define N 100006
 8 int n,m,s;
 9 vector<int>g[N];
10 int flag;
11 int color[N];
12 void dfs(int u,int c,int fa){
13    if(color[u]!=-1){
14       if(color[u]!=c){
15          flag=1;
16       }
17       return;
18    }
19    if(flag) return;
20    color[u]=c;
21    for(int i=0;i<g[u].size();i++){
22       int next=g[u][i];
23       if(next!=fa){
24          dfs(next,!c,u);
25       }
26    }
27 }
28 int main()
29 {
30    int t;
31    int ac=0;
32    scanf("%d",&t);
33    while(t--){
34 
35       scanf("%d%d%d",&n,&m,&s);
36       for(int i=0;i<=n;i++){
37          g[i].clear();
38       }
39       for(int i=0;i<m;i++){
40          int u,v;
41          scanf("%d%d",&u,&v);
42          g[u].push_back(v);
43          g[v].push_back(u);
44       }
45       memset(color,-1,sizeof(color));
46       flag=0;
47       dfs(s,0,-1);
48       printf("Case %d: ",++ac);
49       if(flag){
50          printf("YES\n");
51       }else{
52          printf("NO\n");
53       }
54    }
55     return 0;
56 }
View Code

 


 

CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/5020333.html

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