HDU 3478 Catch(判断奇数环)

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 

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

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

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

#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;
}

/*
2
3 3 0
0 1
0 2
1 2
2 1 0
0 1
*/
时间: 2024-11-08 21:59:05

HDU 3478 Catch(判断奇数环)的相关文章

hdu 3478 Catch(染色 dfs 或 bfs )

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 fr

HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)

XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5304    Accepted Submission(s): 1510 Problem Description It has recently been discovered how to run open-source software on the Y-Crate gam

CSU - 1356 Catch (判奇环)

题目传送门:CSU - 1356 Catch 题目大意: 存在一个n个点m条边的无向图,给定一个出发点,每个时间点能够走到相邻的下个点.能够走重复的边, 问是否存在某一个时间点,他可能停留再任意的n个点之间. 分析: 首先图是联通的,不连通则无法到达一部分点,可以发现如果该图是一条链的话,到达的点分两种情况 (奇数时间到达和偶数时间到达)因此无法满足条件.当图是一个偶数环时,同样无法满足要求,可以分析 只有存在奇数环的时候才能满足再某一时刻处于任何位置.所以题目即判断该图中是否存在奇数环即可 代

hdu 1317 XYZZY(spfa判环)

http://acm.hdu.edu.cn/showproblem.php?pid=1317 大致题意:有n个房间,每个房间都有对应的能量值(可正可负),现在从1出发要到达n,初始能量为100,问是否能够达到n点,到达n的条件是中间及最后的能量值都要大于0. 思路:若不考虑环,那么求最长路判断是否大于0即可.若存在负环,对求最长路也没影响:但当存在正环时,最长路就不存在了.可用spfa判断,当某点入队超过n次,那么它必定在环中,直接将其dis置为INF,并不再将其近队列.最后若能到达n则可行,否

[ACM] hdu 2717 Catch That Cow (BFS)

Catch That Cow Problem Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same

用if else判断奇数偶数.

1 public class 判断奇数偶数 { 2 3 public static void main(String[] args) { 4 5 int num=14; 6 7 if(num%2==0) 8 { 9 System.out.println("num是偶数"); 10 } 11 else 12 { 13 14 System.out.println("num是奇数"); 15 } 16 // TODO 自动生成的方法存根 17 18 } 19 20 } n

【BZOJ1690】【Usaco2007 Dec】奶牛的旅行 分数规划 判断负环

题解: 分数规划+判断负环. 代码: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 1010 #define M 5050 #define eps 1e-8 using namespace std; double mid,fun[N]; struct Eli { int v,n; do

spfa判断负环

会了spfa这么长时间竟然不会判断负环,今天刚回.. [例题]poj3259 题目大意:当农场主 John 在开垦他的农场时,他发现了许多奇怪的昆虫洞.这些昆虫洞是单向的,并且可以把你从入口送到出口,并且使得时间倒退一段时间. John 的每个农场包含 N(1≤N≤500)块地,编号从 1-N,这 N 块地之间有 M(1≤M≤2500)条路. W(1≤W≤200)个昆虫洞.因为 John 是一个狂热的时间旅行迷,他想尝试着做这样一件事:从某块地出发,通过一些路径和昆虫洞,返回到出发点,并且时间早

spfa 判断负环 (转载)

判断给定的有向图中是否存在负环. 利用 spfa 算法判断负环有两种方法: 1) spfa 的 dfs 形式,判断条件是存在一点在一条路径上出现多次. 2) spfa 的 bfs 形式,判断条件是存在一点入队次数大于总顶点数. 代码如下: 法 1 (spfa 的 dfs 形式): #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int oo = 1 &