HDU Catch (odd ring bipartite graph Analyzing + disjoint-set determination Unicom)

 

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

Meaning of the questions: to None of n points m edges, to give a starting point x, from the starting point to the map, each unit is only possible from the point went to the neighboring point, there is not a moment now to ask, you can at any node of FIG.
Ideas: First, if it does not communicate with the FIG., This is not always there, secondly, if the graph is a bipartite graph there is no perfect timing (FIG be divided into two halves, one half is odd arrival time, arrival time of the even half)
Thus, only in FIG communication, and non-bipartite graph when the present time in order to have. Disjoint-set determination by communication, and then determines whether the bipartite graph to FIG.
 
The solution patterned wonders ring bipartite graph modeled staining properties
 
code:
//
#include<bits/stdc++.h>
using namespace std;
#define maxnn 2000110
int n,m,s;
int las[maxnn],nex[maxnn],tot,en[maxnn],col[maxnn],fla=0;
int f[maxnn];
int T;
int  gf(int v)
{
    return f[v]==v?v:f[v]=gf(f[v]);
}
void add(int a,int b)
{
    en[++tot]=b;
    nex[tot]=las[a];
    las[a]=tot;
}
void erfen(int v,int num)
{
    col[v]=num;
    if(fla==1) return ;
    for(int i=las[v];i;i=nex[i])
    {
        int y=en[i];
        if(col[y]==col[v])
        {
            fla=1;
            return ;
        }
        if(!col[y])
        {
            erfen(y,-num);
        }
     } 
     return ;
}
int main()
{
    int x,y,z;
    cin>>T;
    int tot=0;
    while(T--)
    {
        tot++;
        scanf("%d%d%d",&n,&m,&s);
        memset(las,0,sizeof(las));
        memset(col,0,sizeof(col));
        int sec=n;
        for(int i=0;i<n;i++)
            f[i]=i;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
            if(gf(x)!=gf(y))
            {
                f[gf(x)]=gf(y);
                sec--;
            }
        }
        if(sec!=1)
        {
            printf("Case %d: ",tot);
            printf("NO\n");
            continue;
        }
        fla=0;
        erfen(s,1);
        printf("Case %d: ",tot);
        if(fla==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
}

 

Guess you like

Origin www.cnblogs.com/OIEREDSION/p/11272807.html