(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

2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU

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

  

时间: 2024-11-05 17:11:06

(01 染色判奇环) hdu 3478的相关文章

【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 11661   Accepted: 3824 Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, an

[cf557d]Vitaly and Cycle(黑白染色求奇环)

题目大意:给出一个 n 点 m 边的图,问最少加多少边使其能够存在奇环,加最少边的情况数有多少种. 解题关键:黑白染色求奇环,利用数量分析求解. 奇环:含有奇数个点的环. 二分图不存在奇环.反之亦成立. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> using nam

Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)

题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比二班的人数多,每个班的人数都大于零. 解题思路: hdu给出的题解是二分图匹配加上贪心,就不多说了. 还可以用bfs对节点染色,建好图后,对节点进行bfs分成,偶数成与奇数成染成不同的颜色,颜色相同的节点都可以分到同一个集合里面,但是要判断一下奇环,如果出现奇环的话,是无法进行分组的.在每次bfs的

CSU - 1356 Catch (判奇环)

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

Kattis - hoppers Hoppers(判奇环)

题目传送门:Kattis - hoppers  Hoppers 题目大意: 你是一个黑客,现在你发明了一种感染病毒,这种感染病毒能够感染到与该主机相隔一个点的主机. 存在一个无向图,每个点代表一台主机,每条无向边表示两台主机相连,现在你有个计划是通过感 染一个电脑让所有电脑感染,你可以为两条电脑之间加边,需要你求出最少加多少条边能够完成计划 分析: 我们能够发现奇数环中任何一个点被感染都能够使奇数环中所有的点感染.因此我们可以利用该性质. 为了满足条件(感染一个点使所有点被感染)存在几种情况:

[转载]HDU 3478 判断奇环

题意:给定n个点,m条边的无向图(没有重边和子环).从给定点出发,每个时间走到相邻的点,可以走重复的边,相邻时间不能停留在同一点,判断是否存在某个时间停留在任意的n个点. 分析: (1)首先,和出发点的位置没有关系.因为可以走重复的边,且时间没有限制大小. (2)图必须是联通的 (3) 1)图为:2-0-1-3 从0点出发(时间为0),一个时间后到达1或2(时间为1),再一个时间后到达0或3(时间为2)... 可以发现,点分为两类,奇数时间到达和偶数时间到达,答案为NO 2)图为:2-0-1-2

Catch---hdu3478(染色法判断是否含有奇环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 题意:有n个路口,m条街,一小偷某一时刻从路口 s 开始逃跑,下一时刻都跑沿着街跑到另一路口,问是否存在某一时刻出,小偷可能出现在任意路口: 如果小偷能走一个环,如果这个环是偶数个节点,那么某个节点只能在偶数时刻或者奇数时刻到达: 但是如果这个环是奇数个节点,他既可以在奇数时刻到达又可以在偶数时刻到达:所以这道题就是求是否存在一个奇环:如果存在输出YES,否则NO: 由于二分图中不能含有奇环,

Codeforces Round #311 (Div. 2) D. Vitaly and Cycle(二分图染色,奇环)

给定n个点,m条边. 求最少最要加几条边使图中存在奇环,且输出此时加边的方法种数 根据题意,只可能为 0:已经存在奇环,dfs搜到已经染色的且颜色相同 1:判断每个连通块里的 染色黑白色的个数 2 :某个点的 度 > 1 3:0条边 1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<vector> 5 #include<stack> 6 #include

HDU 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 fro