目录

​1,题目描述​

​题目大意​

​输入​

​输出​

​欧拉图​

​2,思路​

​3,AC代码​

​4,解题过程​

​第一搏​

​第二搏​

​第三搏​


1,题目描述

PAT_甲级_1126 Eulerian Path (25point(s)) (C++)【欧拉图】_欧拉图

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

题目大意

判断一个图是欧拉图(Eulerian)、半欧拉图(semi-Eulerian)、非欧拉图(non-Eulerian)。

输入

  1. 第一行:N图的顶点数,M边的数目;
  2. 其余M行,每条边的两个顶点;

输出

  1. 每个节点的度数;
  2. 属于哪种图;

欧拉图

欧拉图是指通过图(​​无向图​​​或​​有向图​​)中所有边且每边仅通过一次通路,相应的回路称为欧拉回路;

具有​​欧拉回路​​的图称为欧拉图(Euler Graph);

具有欧拉通路而无欧拉回路的图称为​​半欧拉图​​。 

 

2,思路

由于是无向图,每条边对应的两个顶点度数加一;

non-Eulerian:非连通图、奇数度节点超过2个;

semi-Eulerian:连通图且奇数度节点为2个;

Eulerian:连通图且无奇数度节点;

 

3,AC代码

#include<bits/stdc++.h>
using namespace std;
bool graph[505][505];
bool visited[505] = {false};
int N, M, v1, v2, oddVer;//N顶点数 M边数 oddVer奇数度节点个数
int degree[505] = {0};
void dfs(int start){
visited[start] = true;
for(int i = 1; i <= N; i++){
if(!visited[i] && graph[start][i] == true)
dfs(i);
}
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
cin>>N>>M;
fill(graph[0], graph[0] + 505 * 505, false);
for(int i = 0; i < M; i++){
scanf("%d %d", &v1, &v2);
degree[v1]++;degree[v2]++;
graph[v1][v2] = graph[v2][v1] = true;
}
for(int i = 1; i <= N; i++){
if(degree[i] % 2 == 1) oddVer++;
printf("%d%c", degree[i], i == N ? '\n':' ');
}
dfs(1);
bool flag = true;//true是连通图 false非连通图
for(int i = 1; i <= N; i++){
if(!visited[i])
flag = false;
}
if(flag){
if(oddVer == 0)
printf("Eulerian");
else if(oddVer == 2)
printf("Semi-Eulerian");
else
printf("Non-Eulerian");
}else printf("Non-Eulerian");
return 0;
}

4,解题过程

第一搏

乍一看题目好害怕,然而看完之后就放心了,,,毕竟结论都给出来了,甚至连图都不用构建,只需要记录每个节点的度数即可。于是:

#include<bits/stdc++.h>
using namespace std;

int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int N, M, v1, v2, oddVer = 0;//N顶点数 M边数 oddVer
cin>>N>>M;
int degree[N+1] = {0};
for(int i = 0; i < M; i++){
scanf("%d %d", &v1, &v2);
degree[v1]++;degree[v2]++;
}
for(int i = 1; i <= N; i++){
printf("%d%c", degree[i], i == N ? '\n':' ');
if(degree[i] % 2 == 1)
oddVer++;
}
if(oddVer == 0)
printf("Eulerian");
else if(oddVer == 2)
printf("Semi-Eulerian");
else
printf("Non-Eulerian");
return 0;
}

PAT_甲级_1126 Eulerian Path (25point(s)) (C++)【欧拉图】_1126_02

第二搏

注意到题目中It has been proven that connected

莫非图不一定是连通图?(这还有点25分的样子)

于是加上了DFS遍历所有连通分量,有一个联通分量是Semi-Eulerian,就将flag设为1,只要有一个连通分量是Non-Eulerian,就将flag设为2,并退出循环。根据flag输出字符串。

#include<bits/stdc++.h>
using namespace std;
bool graph[505][505];
bool visited[505] = {false};
int N, M, v1, v2, oddVer;//N顶点数 M边数 oddVer奇数度节点个数
int degree[505] = {0};
void dfs(int start){
visited[start] = true;
if(degree[start] % 2 == 1)
oddVer++;
for(int i = 1; i <= N; i++){
if(!visited[i] && graph[start][i] == true)
dfs(i);
}
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
cin>>N>>M;
fill(graph[0], graph[0] + 505 * 505, false);
for(int i = 0; i < M; i++){
scanf("%d %d", &v1, &v2);
degree[v1]++;degree[v2]++;
graph[v1][v2] = graph[v2][v1] = true;
}
for(int i = 1; i <= N; i++){
printf("%d%c", degree[i], i == N ? '\n':' ');
}
int flag = 0;
for(int i = 1; i <= N; i++){
oddVer = 0;
if(!visited[i])
dfs(i);
if(oddVer == 2){
flag = 1;
}else if(oddVer > 2){
flag = 2;
break;
}
}
if(flag == 0)
printf("Eulerian");
else if(flag == 1)
printf("Semi-Eulerian");
else
printf("Non-Eulerian");
return 0;
}

PAT_甲级_1126 Eulerian Path (25point(s)) (C++)【欧拉图】_甲级_03

第三搏

再次上网查找答案,发现,只要不是联通图,就一定为Non-Eulerian。这才是这句话It has been proven that connected

PAT_甲级_1126 Eulerian Path (25point(s)) (C++)【欧拉图】_PAT_04