必威体育Betway必威体育官网
当前位置:首页 > IT技术

1126 Eulerian Path(25 分)(C++)

时间:2019-09-07 23:11:04来源:IT技术作者:seo实验室小编阅读:55次「手机版」
 

1126

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 EulerianSemi-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

给定一个图,计算该图的每个结点的度,若所有的节点的度均为偶数则为“Eulerian”,若恰好有两个为奇数则为“Semi-Eulerian”,其他为“Non-Eulerian”。

注意点:需要判断是否是连通图

#include <iOStream>
#include <vector>
using namespace std;
std::vector<int> graph[505];
bool visit[505];
int sum = 0;
void dfs(int index){
    if(visit[index] == true)
        return;
    visit[index] = true;
    ++ sum;
    for(int i = 0; i < graph[index].size(); ++ i)
        dfs(graph[index][i]);
}
int main(){
    int n, m, cnt = 0;
    cin>>n>>m;
    std::vector<int> degree(n + 1, 0);
    for(int i = 0; i < m; ++ i){
        int a, b;
        scanf("%d %d",&a,&b);
        ++ degree[a];
        ++ degree[b];
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    dfs(1);
    for(int i = 1; i <= n; ++ i){
        if(i != 1)
            printf(" ");
        printf("%d",degree[i]);
        if(degree[i] % 2 == 1)
            ++ cnt;
    }
    if(sum == n && cnt == 0)
        printf("\nEulerian");
    else if(sum == n && cnt == 2)
        printf("\nSemi-Eulerian");
    else
        printf("\nNon-Eulerian");
}

相关阅读

[C/C++]洗牌算法

#include <stdio.h>#include <stdlib.h>#include <time.h>int d[6];int i,n,a,b,t;int c,j;void main() {    srand(

谷歌新漏洞:影响了5250万用户的Google+账号

A5创业网(公众号:iadmin5)12月11日消息:谷歌周一表示,将于明年4月关闭Google+社交媒体服务,比原计划提前4个月。此前,该公司今年第二次

卡友支付被罚退出 25省份停止业务、2582万罚款

A5创业网(公众号:iadmin5)10月6日消息,卡友支付宁夏分公司正在退出宁夏银行卡收单市场。此前卡友支付已被正式缩减了业务范围,还被判

C++雾中风景12:聊聊C++中的Mutex,以及拯救生产力的Boos

笔者近期在工作之中编程实现一个Cache结构的封装,需要使用到C++之中的互斥量Mutex,于是花了一些时间进行了调研。(结果对C++标准库很

Navicat连接mysql报错2509

在Navicat中进行连接测试时,发现报错2509,还有乱码!mysql 2509 加密方式导致的报错,在8以后的版本默认的加密方式都改为了caching_sha

分享到:

栏目导航

推荐阅读

热门阅读