UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)

UVA - 10596

Morning Walk
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

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 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Graph :: Special Graphs (Others) ::  Eulerian Graph
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;
} 





你可能感兴趣的:(数据结构,ACM,图论,uva,欧拉回路)