HDU 3478 Catch

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.

题意就是让我们求小偷有没有可能在某一时刻有可能到达所有点。
经典染色

代码:

//首先可以推断出图要是联通的并且要有奇数条边的回路才能输出YES,
//因为从一个点开始沿着一个环分别向左右两个方向遍历,
//只有当前环的结点数为奇数的时候左右两个方向到同一个点是一奇一偶的,
//这样就说明存在某一时刻可以让贼出现在各个位置,
//因为这个点奇偶都可达,那么只需要进行一次BFS并且01染色就可以了
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
#include <cstring>
using namespace std;
int color[110000];//表示第i个点被染的颜色 
int n,m;
vector <int> Map[110000];
queue <int> Que;
bool bfs(int s)//从s点起开始查找当前图Map中有无奇数条边的回路 
{
    while(!Que.empty())
    {
        Que.pop();
    }
    Que.push(s);
    color[s]=0;
    int num=1;
    bool mark=false;
    while(!Que.empty())
    {
        int t=Que.front();
        Que.pop();
        //printf("%lu___\n",Map[t].size());
        for(int i=0;i<Map[t].size();i++)
        {
            int k=Map[t][i];
            //printf("~~~k=%d\n",k);
            if(color[t]==color[k])//如果前一个点后它的后继染色点相同则说明有奇数回路 
            {   mark=true;
                continue;
            }
            if(color[k]==-1)
            {
                num++;
                color[k]=!color[t];
                Que.push(k);
            }
         }
    }
    if(mark&&num==n)//当前图是连通图且不能构成二分图(存在奇数条边的回路) 
        return true;
    return false;
}
int main (void)
{
    int t,s;
    cin>>t;
    int cas=1;
    while(t--)
    {
        scanf("%d %d %d",&n,&m,&s);
        for(int i=0;i<=n;i++)
        {
            Map[i].clear();
        }
        //scanf("%d %d %d",&n,&m,&s);
        while(m--)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            Map[a].push_back(b);
            Map[b].push_back(a);
        }
        memset(color,-1,sizeof(color));
        printf("Case %d: ",cas++);
        if(bfs(s))
        {
            printf("YES\n");
        }
        else
            printf("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值