通路:在无向图中由点边交替组成的序列就是通路(如果这个图是简单的,那么也可以使用点的序列来表示),如果首尾的点相同,则称为一条回路
无向图的连通性:无向图中任意一对点之间均有通路
欧拉通路:从某个顶点出发,将所有的边遍历一遍并且仅经过一遍的通路序列称为欧拉通路,连通的多重图有欧拉回路而无欧拉回路当且仅当它恰有两个奇数度顶点
这里说明了欧拉通路的条件:
- 图是连通的,没有孤立节点
- 对于无向图来说,奇数度的顶点为2个,这两个顶点分别是起点以及终点(0个的话就是回路了)
欧拉回路:如果欧拉通路的起点与终点一样,则成为欧拉回路, 连通的多重图具有欧拉回路当且仅当它的每个顶点都有偶数度
则欧拉回路的条件:
-
图是连通的,没有孤立节点
-
无向图的每个节点的度数都是偶数度,有向图每个节点的入度等于出度
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 Eulerian, Semi-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
#include<bits/stdc++.h>
#include<set>
#include<vector>
#include<map>
using namespace std;
map<int ,int >mp;
int vis[10005];
int par[10005];
int findp(int n)
{
if(par[n]!=n)
n=findp(par[n]);
return n;
}
int main() {
int n,m;
mp.clear();
for(int i=1;i<10005;i++)
par[i]=i;
cin>>n>>m;
int block=n-1;
for(int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
mp[a]++;
mp[b]++;
int A=findp(a);
int B=findp(b);
if(A==B)continue;
par[A]=B;
block--;
}
int even=0,odd=0;
int cnt=0;
for(int i=1;i<=n;i++)
{
if(cnt==0)
cout<<mp[i];
else cout<<" "<<mp[i];
cnt++;
if(mp[i]&1)odd++;
}
cout<<endl;
// cout<<block<<endl;
if(!block&&odd==0)cout<<"Eulerian"<<endl;
else if(!block&&odd==2)cout<<"Semi-Eulerian"<<endl;
else cout<<"Non-Eulerian"<<endl;
}