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;
} 
时间: 2024-08-09 23:52:11

UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)的相关文章

poj 2513 欧拉回路+并查集判断是否联通+Trie树

http://poj.org/problem?id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得以前看过,trie代替map,尤其当数据量特别大的时候 学到了: 1.Trie代替map的思想,可以在单词结尾的tree[i][tk]  这个i作为字符串对应的int值 ,当然这个int值也可以用于建立并查集 2.接上,通过并查集判断,所有的点在同一个集合图就是联通的,否则不联通,注意tree[i][tk]>0 表示是单词结尾, x=Find(x);//这句

uva 10596 Morning Walk(欧拉回路)

这道题是神坑啊,花了我将近3个小时,本来敲的挺顺的,交上去就是wa,坑点真坑,原来是有的路口交叉点可以没有 路通向它,无语,没有路通向也可以叫交叉点....以后一定得考虑多种情况,用了bfs(vector做的)和dfs判断 的是否连通,判断连通之后只需要再判断是否都有偶数个度就ok了,坑,真坑. bfs代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #i

hdu1272小希的迷宫(并查集判断回路和是否连通)

传送门 迷宫中不能有回路,还要连通 如果最后集合数是一个那就是连通,否则不联通 要合并的两个顶点在相同集合内,表示出现了回路 输入时注意一下 1 #include<bits/stdc++.h> 2 using namespace std; 3 int f[100005]; 4 int getf(int v) 5 { 6 if(f[v]==v)return v; 7 else 8 { 9 f[v]=getf(f[v]); 10 return f[v]; 11 } 12 } 13 void mer

uva 10596 Morning Walk (欧拉回路)

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 ble

uva 10596 - Morning Walk

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 ha

uva 11987 Almost Union-Find (并查集)

题目大意: 三个操作. 1. 合并两个集合 2.把第一个元素放到第二个集合里 3.输出集合的数量和和.. 思路分析: 要用p记录这个元素所在集合编号,然后用编号建立并查集. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; int set[111111]; int cn

UVA 1455 - Kingdom(线段树+并查集)

UVA 1455 - Kingdom 题目链接 题意:给定一些城市坐标点,连在一起的城市称为一个州,现在用两种操作,road表示把城市a,b建一条路,line表示询问一个y轴上穿过多少个州,和这些州共包含多少个城市 思路:利用并查集维护每个州的上界和下界还有城市个数,然后每次加进一条路的时候,根据两个集合的位置可以处理出区间的州和城市数如何进行加减,然后利用线段树搞就可以了 代码: #include <cstdio> #include <cstring> #include <

HDU1116(欧拉回路+并查集)

先用并查集来判断图是否连通,然后再根据欧拉回路的出度和入度的性质来判断是否为欧拉回路. 关键是建边,我们可以把字符串看成是一条边,首字母为出发点,尾字母为目的点,建边. #include <stdio.h> #include <string.h> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <math.

hdu 1878 欧拉回路+并查集

欧拉回路: 通过图中每条边且只通过一次,并且经过每一顶点的回路. 无向图欧拉回路的判定:图连通:图中所有节点度均为偶数 有向图欧拉回路的判定:图连通:所有节点入度等于出度 这道题属于无向图,首先用并查集判断图的联通性,各点的度数用一个数组保存下来. 如果一个点的根结点和其他点的根结点不同,则图不联通,有点度数为奇数也不满足欧拉回路,则输出0,否则输出1. 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath&g