PAT:1126. Eulerian Path (25)

1126. Eulerian Path (25)

时间限制
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

初看这道题把我吓一跳,通过率贼低,0.23,把我吓住了。很仔细的看了一遍题目,瞬间反应过来这不就是欧拉图吗。判断所给的图是Eulerian(欧拉图)、Semi-Eulerian(半欧拉图)、Non-Eulerian(非欧拉图)。先回顾一下欧拉图的定义:

欧拉回路:从图的某点出发,经过图的每条边且每条边仅经过一次,最终回到出发点的回路

欧拉通量:从图的某点出发,经过图的每条边且每条边仅经过一次,最终从其他顶点结束的路径

欧拉图:存在欧拉回路的图;

半欧拉图:存在欧拉通量但不存在欧拉回路的图;

一个图是不是欧拉图:如果这个图的每个顶点的度数都是偶数,且只存在一个多顶点的连通分量(因为零度顶点也是偶数),它一定是欧拉图,这是个充要条件。

一个图是不是半欧拉图:如果这个图有且只有两个顶点度数为奇数,且只存在一个多顶点的连通分量,它一定是半欧拉图。这也是充要条件。

现在的问题就是 1.如何计算一个图的连同分量个数?

      2.如何判断仅存在一个多顶点的连通分量?

针对第一个问题,利用DFS(深搜)可以计算连同分量个数。第二个问题,如果连通分量数 = 零度顶点数 + 1,则仅存在一个多顶点的连通分量。上代码:

#include<bits/stdc++.h>
#define MAX 501
using namespace std;
int edge[MAX][MAX];
int dgree[MAX];
bool visit[MAX];
int n,m,zd,even,odd;
void dfs(int f){  //深搜
    visit[f] = true;
    for(int i = 1; i <= n; i++)
        if(edge[f][i] && !visit[i])
            dfs(i);
}
int getPiece(){  //得到这个图的连通分量数目
    int piece = 0;
    for(int i = 1; i <= n; i++){
        if(!visit[i]){
            piece++;
            dfs(i);
        }
    }
    return piece;
}
int main(){
    cin>>n>>m;
    int from,to;
    for(int i = 0; i < m; i++){
        cin>>from>>to;
        edge[from][to] = 1;
        edge[to][from] = 1;
        dgree[from]++;    //记录节点的度数
        dgree[to]++;
    }
    //计算奇数偶数和0度的顶点个数
    for(int i = 1; i <= n; i++){
        if(dgree[i] % 2 == 0){
            even++;
            if(dgree[i] == 0)zd++;
        }else
            odd++;
    }

    for(int i = 1; i < n; i++)
        cout<<dgree[i]<<" ";
    cout<<dgree[n]<<endl;

    //如果边的数目等于零,一定存在欧拉回路(第5个测试用例不过就是这个原因)
    if(m == 0){
        cout<<"Eulerian"<<endl;
        return 0;
    }
    int type; //1表示Eulerian,2表示Semi-Eulerian,3表示Non-Eulerian
    int piece = getPiece();//获得连通分量数目

    //没有奇数度的定点
    if(odd == 0){
        if(zd == 0) //没有0度顶点
            type = (piece == 1 ? 1 : 3);
        else//存在0度顶点
            type = (piece == zd + 1 ? 1 : 3);
    }else if(odd != 0){
        if(odd != 2)//如果奇数度顶点不为2
            type = 3;
        else{
            if(zd == 0) //没有0度顶点
                type = (piece == 1 ? 2 : 3);
            else//存在0度顶点
                type = (piece == zd + 1 ? 2 : 3);
        }
    }

    switch(type){
    case 1:
        cout<<"Eulerian"<<endl;
        break;
    case 2:
        cout<<"Semi-Eulerian"<<endl;
        break;
    case 3:
        cout<<"Non-Eulerian"<<endl;
        break;
    }
}



猜你喜欢

转载自blog.csdn.net/qq_35170267/article/details/78744667
今日推荐