UVa10596 Morning Walk
2015-08-14 20:13
323 查看
UVa10596 Morning Walk
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
nput 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 from 0 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
2 2
0 1
1 0
2 1
0 1
Output for Sample Input
Possible
Not Possible
并查集+欧拉回路的判断
题意: 一个老头每天早上散步, 他想知道能不能走过所有的路, 回到出发点, 但不走重复的路。
思路:欧拉回路存在性判断。 连通、 度数都为偶数。度数判断就不说了。 连通性的判断用并查集,根节点最多只有一个的时候,图是连通的
题目陷阱: 切记m < 2的时候肯定是不能实现的, 要直接输出。
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
nput 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 from 0 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
2 2
0 1
1 0
2 1
0 1
Output for Sample Input
Possible
Not Possible
并查集+欧拉回路的判断
题意: 一个老头每天早上散步, 他想知道能不能走过所有的路, 回到出发点, 但不走重复的路。
思路:欧拉回路存在性判断。 连通、 度数都为偶数。度数判断就不说了。 连通性的判断用并查集,根节点最多只有一个的时候,图是连通的
题目陷阱: 切记m < 2的时候肯定是不能实现的, 要直接输出。
[code]#include<iostream> #include <cstdio> #include <string.h> #define N 210 using namespace std; int n, m, d , u , ran ; void make() { for(int i = 0; i < n; i++) { u[i] = i; ran[i] = 1; } } int find(int x) { if (x != u[x]) { u[x] = find(u[x]); } return u[x]; } void UNion(int x, int y) { x = find(x); y = find(y); if (x == y) return ; if (ran[x] > ran[y]) { u[y] = x; } else { u[x] = y; if (ran[x] == ran[y]) { ran[y]++; } } } int main() { #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); #endif int i, j, x, y; while(~scanf("%d%d", &n, &m)) { bool flag = true; make(); memset(d, 0, sizeof(d)); // memset(out, 0, sizeof(out)); for (i = 0; i < m; i++) { cin >> x >> y; UNion(x, y); d[x]++; d[y]++; } if (!m || n <= 1) { printf("Not Possible\n"); continue; } int root = find(0); for(i = 0; i < n; i++) { if (d[i] != 0) if (find(i) != root || d[i] % 2 != 0) { flag = false; break; } } if (flag) printf("Possible\n"); else printf("Not Possible\n"); } return 0; }
相关文章推荐
- poj 1269
- NZND壳-第三天
- ECMall /app/buyer_groupbuy.app.php SQL Injection Vul
- dotNET使用DRPC远程调用运行在Storm上的Topology
- 利用tcpdump+wireshark对android进行抓包分析
- OC - Property
- 在网站中嵌入视频教学系统
- python3之循环
- JAVA内存泄露原因解析
- 九度oj 1031
- 二分匹配 HDU2063 过山车
- OC - ProtocolAndDelegate
- hdu 1069 Monkey and Banana
- [iOS]NSDate小节
- Http的个人见解
- Redis监控方案
- 笔记(实习第一周)
- CodeForces 14D 树的直径 Two Paths
- Triangle LOVE(拓扑排序)
- 智渔课堂官方免费教程四十五 :Java算法之递归算法