时间限制:300 ms 内存限制:65536 kB 代码长度限制:16000 B 判题程序:Standard 作者:CHEN, Yue
|
---|
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
主要是
邻接链表+dfs
或并查集
判断无向图的连通性,然后结合度的情况判断图的种类邻接链表+dfs
#include <cstdio>
#include <iostream>
#include <list>
using namespace std;
const int N = 505;
void Read(int &vertices, int &edges, int *degrees, list<int> *graphList){
int vertext1,vertext2,i;
scanf("%d%d",&vertices,&edges);
for(i=1;i<=edges;i++){
scanf("%d%d",&vertext1,&vertext2);
//头插法
graphList[vertext1].push_front(vertext2);
graphList[vertext2].push_front(vertext1);
degrees[vertext1] ++;
degrees[vertext2] ++;
}
}
void DFS(int s, list<int> *graphList, bool *flag){
flag[s] = true;
int i;
list<int>::iterator iter;
for(iter=graphList[s].begin();iter!=graphList[s].end();iter++)
if(!flag[*iter]) DFS(*iter,graphList,flag);
}
void GraphCategory(int vertices, int edges, int *degrees, list<int> *graphList){
int i,oddNum,evenNum;
oddNum = evenNum = 0;
for(i=1;i<=vertices;i++){
printf("%d",degrees[i]);
if(i<vertices) printf(" ");
else printf("\n");
if(degrees[i]%2) oddNum ++;
else evenNum ++;
}
/*仅靠vertices-1<=edges是无法判断联通性的,即便先做无环判断,也有问题*/
bool flag[N]={false};
bool ans = true;
DFS(1,graphList,flag);
for(i=1;i<=vertices;i++)
if(!flag[i]){
ans = false;
break;
}
if(ans){ //判断联通性
if(evenNum==vertices) printf("Eulerian\n");
else if(oddNum==2) printf("Semi-Eulerian\n");
else printf("Non-Eulerian\n");
}
else printf("Non-Eulerian\n");
}
int main(){
// freopen("Data.txt","r",stdin);
list<int> graphList[N];
int degrees[N]={0};
int vertices,edges;
Read(vertices,edges,degrees,graphList);
GraphCategory(vertices,edges,degrees,graphList);
return 0;
}
并查集
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 505;
int Find(int vertext, int *root){
while(root[vertext]!=vertext) vertext = root[vertext];
return vertext;
}
bool Read(int &vertices, int &edges, int *degrees){
int vertext1,vertext2,i;
int root[N];
scanf("%d%d",&vertices,&edges);
for(i=1;i<=vertices;i++) root[i] = i; //init
for(i=1;i<=edges;i++){
scanf("%d%d",&vertext1,&vertext2);
degrees[vertext1] ++;
degrees[vertext2] ++;
root[Find(vertext1,root)] = root[Find(vertext2,root)];
}
int count = 0;
for(i=1;i<=vertices;i++)
if(root[i]==i) count ++;
if(count==1) return true;
else return false;
}
void GraphCategory(int vertices, int edges, int *degrees, bool flag){
int i,oddNum,evenNum;
oddNum = evenNum = 0;
for(i=1;i<=vertices;i++){
printf("%d",degrees[i]);
if(i<vertices) printf(" ");
else printf("\n");
if(degrees[i]%2) oddNum ++;
else evenNum ++;
}
if(flag){
if(evenNum==vertices) printf("Eulerian\n");
else if(oddNum==2) printf("Semi-Eulerian\n");
else printf("Non-Eulerian\n");
}
else printf("Non-Eulerian\n");
}
int main(){
// freopen("Data.txt","r",stdin);
int degrees[N]={0};
int vertices,edges;
bool flag;
flag = Read(vertices,edges,degrees);
GraphCategory(vertices,edges,degrees,flag);
return 0;
}