首页 > 其他 > 详细

HDOJ 3478 Catch

时间:2014-03-11 20:51:56      阅读:547      评论:0      收藏:0      [点我收藏+]

二分图染色。。。。

Catch

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


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)
bubuko.com,布布扣
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 <queue>
#include <algorithm>

using namespace std;

const int maxn=110000;

struct Edge
{
    int to,next;
}edge[maxn*10];

int Adj[maxn],Size,n,m,s;
int color[maxn],fa[maxn];

void dsu_init()
{
    for(int i=0;i<n+10;i++) fa[i]=i;
}

int Find(int x)
{
    if(x==fa[x]) return fa[x];
    else return fa[x]=Find(fa[x]);
}

void Union(int a,int b)
{
    int A=Find(a),B=Find(b);
    if(A==B) return ;
    fa[A]=B;
}

void init()
{
    memset(color,-1,sizeof(color));
    memset(Adj,-1,sizeof(Adj));
    Size=0;
}

void Add_Edge(int u,int v)
{
    edge[Size].to=v;
    edge[Size].next=Adj[u];
    Adj[u]=Size++;
}

bool bfs(int x)
{
    queue<int> q;
    q.push(x);
    color[x]=0;

    while(!q.empty())
    {
        int u=q.front(); q.pop();

        for(int i=Adj[u];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(color[v]==-1)
            {
                color[v]=!color[u];
                q.push(v);
            }
            else
            {
                if(color[v]==color[u]) return false;
            }
        }
    }
    return true;
}

int main()
{
    int T_T,cas=1;
    scanf("%d",&T_T);
while(T_T--)
{
    printf("Case %d: ",cas++);
    scanf("%d%d%d",&n,&m,&s);
    init(); dsu_init();
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        Add_Edge(u,v);
        Add_Edge(v,u);
        Union(u,v);
    }

    /// check connect
    int goal=Find(0);
    bool flag=true;
    for(int i=1;i<n;i++)
    {
        if(Find(i)!=goal)
        {
            printf("NO\n");
            flag=false; break;
        }
    }
    if(flag==false) continue;

    ///got color
    if(bfs(s)) puts("NO");
    else puts("YES");
}
    return 0;
}



HDOJ 3478 Catch,布布扣,bubuko.com

HDOJ 3478 Catch

原文:http://blog.csdn.net/u012797220/article/details/20959147

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!