HDU 3478 Catch(判断奇数环)

来源:互联网 发布:怎么删除mac中系统文件 编辑:程序博客网 时间:2024/06/14 14:27

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

23 3 00 10 21 22 1 00 1
 

Sample Output

Case 1: YESCase 2: NO

题意:判断是否存在一个时候任何点都可达。

奇数环存在意义就是可以改变奇偶性。奇数走奇数环变偶数,偶数走奇数环变奇数。

所以有奇数环条件就可以满足。判断方法是经典的染色法。

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;const int maxn=1e5+100;int t,n,m,st,flag;int head[maxn],cnt;struct node{    int u,v;    int next;}e[maxn*10];int col[maxn];void init(){    cnt=0;    CLEAR(head,-1);    CLEAR(col,-1);}void addedge(int u,int v){    e[cnt].u=u;    e[cnt].v=v;    e[cnt].next=head[u];    head[u]=cnt++;}bool BFS(){    queue<int>q;    q.push(st);    col[st]=1;    while(!q.empty())    {        int xx=q.front();        q.pop();        for(int i=head[xx];i!=-1;i=e[i].next)        {            int to=e[i].v;            if(col[to]==-1)            {                q.push(to);                col[to]=!col[xx];            }            else if(col[to]==col[xx])                return true;        }    }    return false;}int main(){    int x,y;    int cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&m,&st);        init();        while(m--)        {            scanf("%d%d",&x,&y);            addedge(x,y);addedge(y,x);        }        if(BFS()) printf("Case %d: YES\n",cas++);        else  printf("Case %d: NO\n",cas++);    }    return 0;}/*23 3 00 10 21 22 1 00 1*/


0 0