码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10596 Morning Walk (欧拉回路)

时间:2015-01-27 11:09:02      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:uva   c++   

                       uva 10596 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

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 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

Output for Sample Input

2 2

0 1

1 0

2 1

0 1

Possible

Not Possible




题目大意:判断所给出的数据能否形成一个环。

解题思路:普通欧拉回路。


>
#include<string.h>
int num[250], gra[250];
int get(int x) {
	return num[x] != x ? get(num[x]) : x;
}
int main() {
	int n, m;
	while (scanf("%d %d\n", &n, &m) == 2) {
		memset(gra, 0, sizeof(gra));
		for (int i = 0; i < 250; i++) {
			num[i] = i;
		}
		for (int i = 0; i < m; i++) {
			int a, b;
			scanf("%d %d", &a, &b);
			gra[a]++;
			gra[b]++;
			num[get(a)] = get(b);
		}
		int begin = 0, flag = 0;
		for (int i = 0; i < 250; i++) {
			if (gra[i] && get(i) == i) {
				begin = i;
				break;
			}
		}
		for (int i = 0; i < n; i++) {
			flag += gra[i] % 2;
			if (gra[i] && begin != get(i)) flag++;
		}
		if (m == 0) flag = 1;  //注意特殊情况,数据会卡这里
		if (flag)  
			printf("Not Possible\n");  
		else  
			printf("Possible\n");  
	}
	return 0;
}




uva 10596 Morning Walk (欧拉回路)

标签:uva   c++   

原文地址:http://blog.csdn.net/llx523113241/article/details/43191967

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!