(01 染色判奇环) hdu 3478

Catch

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1296    Accepted Submission(s): 632


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

 

Source
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<queue>
using namespace std;
vector<int> map[100001];
int vis[100010];
int n,m,s;
bool flag=false;
void bfs()
{
    queue<int> q;
    q.push(s);
    vis[s]=0;
    int p;
    while(!q.empty())
    {
        p=q.front();
        q.pop();
        for(int i=0;i<map[p].size();i++)
        {
            if(vis[map[p][i]]==-1)
            {
                if(vis[p]==1)
                    vis[map[p][i]]=0;
                else
                    vis[map[p][i]]=1;
                q.push(map[p][i]);
            }
            else
            {
                  if(vis[map[p][i]]==vis[p])
                        flag=true;
            }
        }
}
int main()
{
    int T,cas;
    scanf("%d",&T);
    for(cas=1;cas<=T;cas++)
    {
        scanf("%d%d%d",&n,&m,&s);
        memset(vis,-1,sizeof(vis));
        flag=false;
        for(int i=0;i<n;i++)
            map[i].clear();
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            map[a].push_back(b);
            map[b].push_back(a);
        }
        bfs();
        printf("Case %d:",cas);
        if(flag)
            printf(" YES\n");
        else
            printf(" NO\n");
    }
    return 0;
}

  

posted @ 2015-01-25 09:50  心田定则  阅读(361)  评论(0编辑  收藏  举报