UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)
来源:互联网 发布:无印良品 知乎 编辑:程序博客网 时间:2024/05/18 00:07
UVA - 10596
Submit Status
Description
Problem H
Morning Walk
Time Limit
3 Seconds
Kamal is a Motashota guy. He has got a new job in Chittagong . So, he has moved to Chittagong from Dinajpur. He was getting fatter in Dinajpur as he had no work in his hand there. So, moving to Chittagong has turned to be a blessing for him. Every morning he takes a walk through the hilly roads of charming city Chittagong . He is enjoying this city very much. There are so many roads in Chittagong and every morning he takes different paths for his walking. But while choosing a path he makes sure he does not visit a road twice not even in his way back home. An intersection point of a road is not considered as the part of the road. In a sunny morning, he was thinking about how it would be if he could visit all the roads of the city in a single walk. Your task is to help Kamal in determining whether it is possible for him or not.
Input
Input will consist of several test cases. Each test case will start with a line containing two numbers. The first number indicates the number of road intersections and is denoted by N (2 ≤ N ≤ 200). The road intersections are assumed to be numbered from0 to N-1. The second number R denotes the number of roads (0 ≤ R ≤ 10000). Then there will be R lines each containing two numbers c1 and c2 indicating the intersections connecting a road.
Output
Print a single line containing the text “Possible” without quotes if it is possible for Kamal to visit all the roads exactly once in a single walk otherwise print “Not Possible”.
Sample Input
Output for Sample Input
2 2
0 1
1 0
2 1
0 1
Possible
Not Possible
Problemsetter: Muhammad Abul Hasan
International Islamic University Chittagong
Source
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Graph :: Special Graphs (Others) :: Eulerian Graph
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 2. Data Structures :: Graphs
思路:先判断图是否连通,在判断图上每个点的度是否都为偶数,用DFS或BFS或并查集都可。(博主我DFS没搞出来。。以后再想想)
AC代码1:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxv = 205;int deg[maxv];int fa[maxv];int find(int x){if(x != fa[x])fa[x] = find(fa[x]);return fa[x];}int main(){int n, r;while(scanf("%d %d", &n, &r)!=EOF){if(r == 0){ printf("Not Possible\n"); continue; } //不存在路,直接输出,不然会RE memset(deg, 0, sizeof(deg));for(int i=0; i<n; i++){fa[i] = i;}//输入 for(int i=0; i<r; i++){int a, b;scanf("%d %d", &a, &b);if(find(a)!=find(b))fa[find(a)] = find(b);deg[a]++; deg[b]++;}// 判断是否是连通的 int ok = 0, k;for(k=0; !deg[k]; k++);for(int j=k+1; j<n; j++)if(deg[j] && find(k) != find(j)){ok = 1;break;}//判断每个节点的度是否都是偶数 int ju = 0;if(!ok)for(int i=0; i<n; i++)if(deg[i]%2 != 0){ju = 1;break;}if(ok || ju) printf("Not Possible\n");else printf("Possible\n");}return 0;}
- UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)
- UVa 10596 - Morning Walk(欧拉回路)
- UVa 10596 Morning Walk ( 欧拉回路 )
- Morning Walk - UVa 10596 欧拉回路
- uva 10596 Morning Walk(欧拉回路)
- Uva 10596 - Morning Walk 欧拉回路基础水题 并查集实现【uva数据修改,代码已更新】
- uva 10596 Morning Walk(欧拉回路)
- uva 10596 - Morning Walk (欧拉回路~~~)
- UVA - 10596 Morning Walk (欧拉回路+dfs)
- uva 10596 Morning Walk (欧拉回路)
- UVa 10596 - Morning Walk, 赤裸裸的欧拉回路
- UVA 10596 Morning Walk 简单的k欧拉回路
- UVA 10196 Morning Walk(欧拉回路)
- 10596 - Morning Walk//欧拉回路
- 10596 - Morning Walk(欧拉回路 + dfs)
- UVa 10596 - Morning Walk(无向图,欧拉回路)
- Morning Walk-uva 经典的欧拉回路
- UVa 10596 - Morning Walk 有向图的欧拉回路
- 2014年4月读书单
- 浅谈网页搜索排序中的投票模型
- cadence中画焊盘注意事项
- nyoj1168An Easy Problem
- struts2的s:param标签中文乱码问题解决
- UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)
- Lua的__index
- bestcoder#22NPY and girls
- php判断文件上传类型及过滤不安全数据的方法
- 目前最细致清晰的NSDictionary以及NSMutableDictionary用法总结
- php可生成缩略图的文件上传类实例
- nyoj816它合法吗?
- fdopen()和fileno()函数
- VB 通用对话框 为什么需要 CancelError