[HDU] [1878] Eulerian Circuit

Problem Description

Loading...

Problem Analysis

Determine whether an Eulerian circuit exists

  1. Every vertex must have an even number of connections
  2. There must be only one root

So Union-Find can handle it easily

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
int par[1020];
int r[1020];
int find(int m) {
	if(par[m]==m)
		return m;
	else
		return par[m]=find(par[m]);
}
void unite(int x,int y) {
	x=find(x);
	y=find(y);


	par[y]=x;
}
int main() {
	int n;
	while(scanf("%d",&n),n) {
		int m;
		scanf("%d",&m);
		for(int i=1; i<=n; i++) {
			r[i]=0;
			par[i]=i;
		}
		while(m--) {
			int x,y;
			scanf("%d %d",&x,&y);
			unite(x,y);
			r[x]++;
			r[y]++;
		}
		int cnt=0;
		for(int i=1; i<=n; i++)
			if(i==find(i))
				cnt++;
		if(cnt!=1) {
			printf("0\n");
			continue;
		}
		int i;
		for(i=1; i<=n; i++) {
			if(r[i]&1)
				break;
		}
		if(i<=n)
			printf("0\n");
		else
			printf("1\n");
	}
	return 0;
}

Problem link:HDU 1878 Eulerian Circuit

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...