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

解决Kamal在Chittagong城市中寻找一次访问所有道路路径的问题,通过判断图的连通性和节点度数来确定可能性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UVA - 10596

Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu

Submit Status

Description

Download as PDF

 

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值