HDU 3478 Catch(判断奇数环)
来源:互联网 发布:怎么删除mac中系统文件 编辑:程序博客网 时间:2024/06/14 14:27
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.
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.
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
23 3 00 10 21 22 1 00 1
Sample Output
Case 1: YESCase 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;}/*23 3 00 10 21 22 1 00 1*/
0 0
- HDU 3478 Catch(判断奇数环)
- hdu 3478 Catch(判断奇数环)
- hdu 3478 Catch 二分图判断
- hdu 5215 Cycle(判断是否有奇数偶数的环)
- csu 1356 Catch (判断奇环)
- Catch hdu 3478
- hdu 3478 Catch
- HDU 3478 Catch
- 判断奇数
- 奇数判断
- 【染色法】hdu 3478 Catch
- HDU 2531 Catch him(BFS:判断是否存在路径)
- (hdu(1998)奇数阶魔方
- HDU-2006(求奇数的乘积)
- csu 1356: Catch(判断奇圈)
- CodeForces 216B Forming Teams(并查集判断奇数环)
- asp.net奇数判断
- 判断奇数偶数
- 关于NFC芯片选择的一些记录
- Android 网络通信框架Volley简介(Google IO 2013)
- 相机标定中个函数简介
- 挖一挖unsigned int和补码
- c语言学习之浅尝指针
- HDU 3478 Catch(判断奇数环)
- KMP子字符串查找算法
- ASP.NET MVC 4 Web 编程-Chap02 ASP.NET Web Form 开发人员必读
- 备份工具Amanda
- H5 webapp 嵌套Android 和 iOS 那些坑
- hdu 1050 Moving Tables
- MFC中获取命令行参数的几种方法
- C编译器剖析_6.3.6 汇编代码生成_为“取地址”产生汇编指令
- Palindrome Number