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 to N–.
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 + 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 (≤ ), M (≤ ), 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 integers u and v in each line ( ≤ 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

Sample Output

Case : YES
Case : NO

Hint

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

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

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

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

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

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

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

第一种写法,和hdu4751比较

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
int n,m,s;
vector<int> g[N];
int color[N];
bool dfs(int u,int c){
color[u]=c;
for(int i=;i<g[u].size();i++){
int next=g[u][i];
if(color[next]!=-){
if(color[next]==c){
return true;
}
continue;
}
if(dfs(next,!c)) return true;
}
return false;
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){
for(int i=;i<N;i++){
g[i].clear();
}
scanf("%d%d%d",&n,&m,&s);
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(color,-,sizeof(color));
printf("Case %d: ",++ac);
if(dfs(s,)){
printf("YES\n");
}else{
printf("NO\n");
}
}
return ;
}

第二种写法:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define N 100006
int n,m,s;
vector<int>g[N];
int flag;
int color[N];
void dfs(int u,int c,int fa){
if(color[u]!=-){
if(color[u]!=c){
flag=;
}
return;
}
if(flag) return;
color[u]=c;
for(int i=;i<g[u].size();i++){
int next=g[u][i];
if(next!=fa){
dfs(next,!c,u);
}
}
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){ scanf("%d%d%d",&n,&m,&s);
for(int i=;i<=n;i++){
g[i].clear();
}
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(color,-,sizeof(color));
flag=;
dfs(s,,-);
printf("Case %d: ",++ac);
if(flag){
printf("YES\n");
}else{
printf("NO\n");
}
}
return ;
}
04-15 07:35