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.
even 偶数,odd 奇数
Eulerian 连通图,所有顶点度为偶
semi-Eulerian 连通图,有两个顶点度为奇
判断连通图,一个深搜:
如果遍历的点的个数不等于点的总数,那么说明至少有两个连通分支,图不是连通图
void dfs(int x){
visit[x]=true;
cnt++;
for(int i=0;i<v[x].size();i++){
if(visit[v[x][i]]==false) dfs(v[x][i]);
}
}
// even degree odd degree
#include<bits/stdc++.h>
using namespace std;
const int maxn=510;
vector<int> v[maxn];
bool visit[maxn]={false};
int cnt=0;
void dfs(int x){
visit[x]=true;
cnt++;
for(int i=0;i<v[x].size();i++){
if(visit[v[x][i]]==false) dfs(v[x][i]);
}
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
while(m--){
int x,y;
scanf("%d %d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1);
int odd=0,even=0;
for(int i=1;i<=n;i++){
if(i!=1) printf(" ");
printf("%d",v[i].size());
if(v[i].size()%2==0) even++;
else odd++;
}
printf("\n");
if(even==n&&cnt==n){
printf("Eulerian");
}else if(odd==2&&cnt==n){
printf("Semi-Eulerian");
}else{
printf("Non-Eulerian");
}
return 0;
}