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 inChittagong 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 Kamalin 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 andc2 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<stdio.h>
#include<string.h>
#define M 205
int num[M], cnt[M];
int n, m, bo;

int get_fa(int x){
	return num[x] != x?get_fa(num[x]):x;}

int main(){
	while (scanf("%d%d", &n, &m) != EOF){
		// Init.
		memset(cnt, 0, sizeof(cnt));
		for (int i = 0; i < n; i++)
			num[i] = i;
		bo = 0;
        
		// Read.
		for (int i = 0; i < m; i++){
			int a, b;
			scanf("%d%d", &a, &b);
			cnt[a]++;
			cnt[b]++;
            num[get_fa(a)] = get_fa(b);
		}
        
		// Find.
		int god = 0;
		for (int i = 0; i < M; i++)
			if (cnt[i] && get_fa(i) == i)
			{
				god = i;
				break;
			}

		// Judge.
		for (int i = 0; i < n; i++){
			bo += cnt[i] % 2;
			if (cnt && god != get_fa(i))
				bo++;
		}

		// Printf.
		if (bo > 0)
			printf("Not Possible\n");
		else
			printf("Possible\n");
	}
	return 0;}

你可能感兴趣的:(uva 10596 Morning Walk(欧拉回路))