UVa10596 Morning Walk

来源:互联网 发布:淘宝模特拍照动作技巧 编辑:程序博客网 时间:2024/06/06 14:07

 

Problem H

Morning Walk

Time Limit

3 Seconds

 

Kamalis a Motashotaguy. He has got a new job in Chittagong.So, he has moved to Chittagong fromDinajpur. He was getting fatter in Dinajpur as he had no work in his hand there. So, moving toChittagong has turned to be ablessing for him. Every morning he takes a walk through the hilly roads ofcharming city Chittagong. He isenjoying this city very much. There are so many roads in Chittagongand every morning he takes different paths for his walking. But while choosinga path he makes sure he does not visit a road twice not even in his way backhome. An intersection point of a road is not considered as the part of theroad. In a sunny morning, he was thinking about how it would be if he couldvisit 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 severaltest cases. Each test case will start with a line containing two numbers. Thefirst number indicates the number of road intersections and is denoted byN(2 ≤ N ≤ 200). The road intersections are assumed to benumbered from0 to N-1. The second number R denotes thenumber of roads (0 ≤ R ≤ 10000). Then there will beRlines each containing two numbers c1 andc2indicating the intersections connecting a road.

 

Output

Print a single line containingthe text “Possible” without quotes if it is possible for Kamal to visit all the roads exactly once in a single walkotherwise print “Not Possible”.

 

Sample Input

Output for Sample Input

2 2

0 1

1 0

2 1

0 1

Possible

Not Possible

 

Problemsetter: MuhammadAbul Hasan

International IslamicUniversity Chittagong


这题大意就是一个人,出来溜弯,面对这些街道,他在想能不能从一个地方出发,然后经过所有路口且都只经过一次,然后会到原来的地方。其实已经讲很明白了,就是一道欧拉回路的问题,用并查集连结各点,判断连通性,再以各点的度数判断是否为欧拉回路,具体也可以看下欧拉回路的公式。

#include<iostream>#include<cstring>using namespace std;const int N = 202;int n;int m;int graph[N][N];int degree[N];int father[N];void initialize(){for (int i = 0; i < n; i++)father[i] = i;}int ancestor(int x){return father[x] == x?x:father[x] = ancestor(father[x]);}bool union_find_sets(){initialize();for (int i = 0; i < n; i++){for (int j = 0; j <= i; j++){if (graph[i][j]){int ai = ancestor(i);int aj = ancestor(j);if (ai != aj)father[ai] = aj;}}}int ff = ancestor(0);for (int i = 1; i < n; i++){if (father[i] != ff)return false;}return true;}bool euler(){for (int i = 0; i < n; i++){if (degree[i] % 2)return false;}return true;}int main(){while (cin >> n >> m){memset(graph,0,sizeof(graph));memset(degree,0,sizeof(degree));for (int i = 0; i < n; i++)graph[i][i] = 1;int x;int y;for (int i = 0; i < m; i++){cin >> x >> y;graph[x][y]++;graph[y][x]++;degree[x]++;degree[y]++;}if (union_find_sets() && euler())cout << "Possible" << endl;elsecout << "Not Possible" << endl;}return 0;}


原创粉丝点击