HDOJ 题目3478 Catch(染色法判二分图)
来源:互联网 发布:飞利浦脱毛器 知乎 编辑:程序博客网 时间:2024/05/16 12:04
Catch
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1299 Accepted Submission(s): 635
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.
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: NOHintFor 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
Recommend
zhengfeng | We have carefully selected several similar problems for you: 3486 3484 3483 3482 3481
ac代码
不判联通也能过
#include<stdio.h>#include<string.h>#include<stdlib.h>int s,n,m,cnt,ans;int head[100010],vis[100010],color[100010];struct s{int u,v,next;}edge[500010*2];void add(int u,int v){edge[cnt].u=u;edge[cnt].v=v;edge[cnt].next=head[u];head[u]=cnt++;}void dfs(int u){vis[u]=1;ans++;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].v;if(vis[v]==0)dfs(v);}}int link(){ans=0;memset(vis,0,sizeof(vis));dfs(0);if(ans==n)return 1;return 0;}int dfs(int u,int c){if(!color[u]){color[u]=c;}else{if(color[u]==c)return 0;return 1;}for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].v;if(dfs(v,-c))return 1;}return 0;}int jud(){memset(color,0,sizeof(color));int i;for(i=0;i<n;i++){if(!color[i])if(dfs(i,1))return 0;}return 1;}int main(){int t,c=0;scanf("%d",&t);while(t--){int i;scanf("%d%d%d",&n,&m,&s);memset(head,-1,sizeof(head));cnt=0;for(i=0;i<m;i++){int u,v;scanf("%d%d",&u,&v);add(u,v);add(v,u);}if(link()&&jud()==0){printf("Case %d: YES\n",++c);}elseprintf("Case %d: NO\n",++c);}}
0 0
- HDOJ 题目3478 Catch(染色法判二分图)
- HDOJ题目4751 Divide Groups(染色法判二分图)
- HDOJ 题目2444 The Accomodation of Students(染色法判二分图,最大匹配)
- NYOJ 题目1015 二部图(染色法判二分图)
- HDU 3478 Catch【kurskal+二分图染色】
- hdoj 2444 The Accomodation of Students 【黑白染色判二分图 + 最大匹配】
- hdu 3478 Catch【并查集+二分图染色】
- HDU-3478 Catch(二分图染色+并查集)
- 3478 Catch (判断是否为二分图,dfs奇偶染色)
- HDU2444_The Accomodation of Students _染色法判二分图&邻接矩阵二分图匹配模板
- HDU 2444 黑白染色判二分图+二分最大匹配
- POJ 2942 Knights of the Round Table (点-双连通分量 + 交叉法染色判二分图)
- HDOJ 题目3594 Cactus(强连通,判仙人掌图)
- 【染色法】hdu 3478 Catch
- hdu5971 Wrestling Match(染色法判二分) 2016ACM/ICPC亚洲区大连站
- poj2942(双联通分量,交叉染色判二分图)
- poj 3207 2-sat 或 染色判二分图
- HDU 4751 — Divide Groups 交叉染色判二分图
- office2013制作网页
- C++ STL之vector详解
- javascript中的length属性
- 改善C#程序的建议9:使用Task代替ThreadPool和Thread
- 文章标题
- HDOJ 题目3478 Catch(染色法判二分图)
- 题目1182:统计单词
- 内存体系 用共享段于进程间联系
- next_permutation函数的使用
- 使用javah编译生成c的.h文件
- boost property_tree 解析json文件
- java学习笔记
- Info.plist与Prefix.pch修改文件位置遇到的问题及解决方法
- 【Javaweb】Eclipse for JavaEE新建的Web工程自动生成web.xml