PAT (Advanced Level) Practice 1126 Eulerian Path (25分) (欧拉路、欧拉回路的判断 DFS/并查集加优化)

1.题目

In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were first discussed by Leonhard Euler while solving the famous Seven Bridges of Konigsberg problem in 1736. It has been proven that connected graphs with all vertices of even degree have an Eulerian circuit, and such graphs are called Eulerian. If there are exactly two vertices of odd degree, all Eulerian paths start at one of them and end at the other. A graph that has an Eulerian path but not an Eulerian circuit is called semi-Eulerian. (Cited from https://en.wikipedia.org/wiki/Eulerian_path)

Given an undirected graph, you are supposed to tell if it is Eulerian, semi-Eulerian, or non-Eulerian.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N (≤ 500), and M, which are the total number of vertices, and the number of edges, respectively. Then M lines follow, each describes an edge by giving the two ends of the edge (the vertices are numbered from 1 to N).

Output Specification:

For each test case, first print in a line the degrees of the vertices in ascending order of their indices. Then in the next line print your conclusion about the graph -- either EulerianSemi-Eulerian, or Non-Eulerian. Note that all the numbers in the first line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

Sample Input 1:

7 12
5 7
1 2
1 3
2 3
2 4
3 4
5 2
7 6
6 3
4 5
6 4
5 6

Sample Output 1:

2 4 4 4 4 4 2
Eulerian

Sample Input 2:

6 10
1 2
1 3
2 3
2 4
3 4
5 2
6 3
4 5
6 4
5 6

Sample Output 2:

2 4 4 4 3 3
Semi-Eulerian

Sample Input 3:

5 8
1 2
2 5
5 4
4 1
1 3
3 2
3 4
5 3

Sample Output 3:

3 3 4 3 3
Non-Eulerian

2.题目分析

1.欧拉回路判断:一个无向图存在欧拉回路,当且仅当该图所有顶点度数都为偶数,且该图是连通图。  

2.欧拉路判断:顶点度数只有两个是奇数剩下的都是偶数且该图是连通图

3.题目中并查集方法中使用了路径压缩、按秩合并的优化策略

3.代码

DFS:

#include<iostream>
using namespace std;
int edges[501][501];
int in[501];
int visited[501];
void DFS(int v, int n)
{
	visited[v] = 1;
	for (int i = 1; i <= n; i++)
		if (edges[v][i] != 0 && visited[i] == 0)
			DFS(i, n);
}

int main()
{
	int n, m, a, b, oddcount = 0;
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= m; i++)
	{
		scanf("%d %d", &a, &b);
		edges[a][b] = edges[b][a] = 1;
		in[a]++; in[b]++;
	}
	for (int i = 1; i <= n; i++)
		if (in[i] % 2 != 0)oddcount++;
	for (int i = 1; i <= n; i++)
		printf("%s%d", i == 1 ? "" : " ", in[i]);
	printf("\n");
	DFS(1, n);
	bool Eulerian = true;
	for (int i = 1; i <= n; i++)
	{
		if (visited[i] == 0) { Eulerian = false; break; }
	}
	if (!Eulerian || (oddcount != 2&&oddcount!=0))printf("Non-Eulerian\n");
	if (Eulerian&&oddcount == 0)printf("Eulerian\n");
	if (Eulerian&&oddcount == 2)printf("Semi-Eulerian\n");
}

 并查集:

#include<iostream>
using namespace std;
int edges[501][501];
int in[501];
int father[501],ranks[501];
int find(int x)
{
	if (father[x] == x)
		return father[x];
	return father[x]= find(father[x]);
}
void unions(int x, int y)
{
	int a = find(x);
	int b = find(y);
	if (a == b)return;
	if (ranks[a] > ranks[b])father[b] = a;
	else
	{
		father[a] = b;
		if (ranks[a] == ranks[b])ranks[b]++;
	}
}
int main()
{
	int n, m, a, b, oddcount = 0;
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
		father[i] = i;
	for (int i = 1; i <= m; i++)
	{
		scanf("%d %d", &a, &b);
		edges[a][b] = edges[b][a] = 1;
		in[a]++; in[b]++;
		unions(a, b);
	}
	for (int i = 1; i <= n; i++)
		if (in[i] % 2 != 0)oddcount++;
	for (int i = 1; i <= n; i++)
		printf("%s%d", i == 1 ? "" : " ", in[i]);
	printf("\n");
	int con = 0;
	for (int i = 1; i <= n; i++)
		if (father[i] == i) con++;
	if (con!=1 || (oddcount != 2 && oddcount != 0))printf("Non-Eulerian\n");
	if (con==1&&oddcount == 0)printf("Eulerian\n");
	if (con==1&&oddcount == 2)printf("Semi-Eulerian\n");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值