HDU 3478
2015-08-09 20:44
274 查看
Catch
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1389 Accepted Submission(s): 682
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
//染色 判断二分
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1389 Accepted Submission(s): 682
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
//染色 判断二分
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struct Node { int v; int next; }Edge[500010*2]; int pre[100010]; void add(int u,int v,int index) { Edge[index].v=v; Edge[index].next=pre[u]; pre[u]=index; } int flag[100010]; int c[100010]; int bfs(int v) { queue <int> q; flag[v]=1; q.push(v); while(!q.empty()) { int b=q.front(); q.pop(); for(int i=pre[b];i!=-1;i=Edge[i].next) { int e=Edge[i].v; if(flag[e]==flag[b]) return 0; if(!flag[e]) { flag[e]=flag[b]==1?2:1; q.push(e); } } } return 1; } int main() { int n,m,q,cnt=1; int t; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&q); int a,b,index=1; memset(flag,0,sizeof(flag)); memset(pre,-1,sizeof(pre)); memset(c,0,sizeof(c)); for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); add(a,b,index++); add(b,a,index++); c[a]++,c[b]++; } int state=0; for(int i=1;i<=n;i++) { if(!flag[i]&&c[i]) { if(!bfs(i)) { state=1; break; } } } printf("Case %d: ",cnt++); if(state) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- 第一章 绪论
- 高并发处理方案
- “||”操作符小用
- 队列笔记
- IM-即时通讯技术概述
- HDU 2444
- Android四大组件之三:ContentProvider数据共享
- unbuntu下如何更新adobe flash插件
- IM-即时通讯技术概述
- 协议详解3——IP
- PyQt5初级教程--PyQt5中的拖放[10/13]
- HDU 5366
- 【C/C++学院】0726-cppIDE/一级指针/指针数组/函数指针/函数指针数组/二级指针
- Flatten Binary Tree to Linked List
- Spring注解
- 顿悟,职业生涯最重要的是行业水平的积累,而不是多学某一门语言(很危险)——遥想铁血强人刘志军对铁路行业的理解以及执行力
- 使用Jayrock开源组件创建参数可为空的接口
- Program D--贪心-区间覆盖
- 图片下载缓存防止OOM
- Sqrt(x)