PAT_A1126#Eulerian Path

Source:

PAT A1126 Eulerian Path (25 分)

Description:

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

Keys:

  • 图的存储和遍历

Attention:

  • 判断图的连通性与顶点度的奇偶性即可

Code:

 1 /*
 2 Data: 2019-06-01 19:33:52
 3 Problem: PAT_A1126#Eulerian Path
 4 AC: 56:58
 5
 6 题目大意:
 7 欧拉路径可以访问图中所有的边且各边仅访问一次,欧拉回路是起点和终点相同的欧拉路径;
 8 已知各顶点均含有偶数条边的图可构成欧拉回路,该图称作欧拉图;
 9 若只有两个顶点含有奇数条边的图可构成欧拉路径,并且这两个结点作为欧拉路径的起点和终点;
10 含有欧拉路径但不含欧拉回路的图,称作半欧拉图
11 现在给定一个图,判断其是否为欧拉图
12 输入:
13 第一行给出,顶点数N<=500,边数M
14 接下来M行给出各边
15 输出:
16 第一行给出,各顶点边数
17 第二行给出,非欧拉图,半欧拉图,欧拉图
18 */
19
20 #include<cstdio>
21 #include<algorithm>
22 using namespace std;
23 const int M=510,INF=1e9;
24 int grap[M][M],vis[M],in[M],n,sum=0;
25
26 void DFS(int v)
27 {
28     vis[v]=1;
29     sum++;
30     for(int u=1; u<=n; u++)
31         if(vis[u]==0 && grap[u][v]!=INF)
32             DFS(u);
33 }
34
35 int main()
36 {
37 #ifdef    ONLINE_JUDGE
38 #else
39     freopen("Test.txt", "r", stdin);
40 #endif
41
42     fill(in,in+M,0);
43     fill(vis,vis+M,0);
44     fill(grap[0],grap[0]+M*M,INF);
45     int m,v1,v2,cnt=0;
46     scanf("%d%d", &n,&m);
47     for(int i=0; i<m; i++)
48     {
49         scanf("%d%d", &v1,&v2);
50         grap[v1][v2]=1;
51         grap[v2][v1]=1;
52         in[v1]++;
53         in[v2]++;
54     }
55     for(int i=1; i<=n; i++){
56         printf("%d%c", in[i], i==n?‘\n‘:‘ ‘);
57         if(in[i]%2==1)  cnt++;
58     }
59     DFS(1);
60     if(cnt==2 && sum==n)
61             printf("Semi-Eulerian\n");
62     else if(cnt==0 && sum==n)
63             printf("Eulerian\n");
64     else    printf("Non-Eulerian\n");
65
66
67     return 0;
68 }

原文地址:https://www.cnblogs.com/blue-lin/p/10960945.html

时间: 2024-07-31 18:54:49

PAT_A1126#Eulerian Path的相关文章

Graph | Eulerian path

In graph theory, a Eulerian trail (or Eulerian path) is a trail in a graph which visits every edge exactly once. Similarly, an Eulerian circuit or Eulerian cycle is an Eulerian trail which starts and ends on the same vertex. Hierholzer's algorithm 可以

PAT 1126 Eulerian Path

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 fa

PAT 甲级 1126 Eulerian Path

https://pintia.cn/problem-sets/994805342720868352/problems/994805349851185152 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

PAT A1126 Eulerian Path (25 分)

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 fa

1126 Eulerian Path (25 分)

1126 Eulerian Path (25 分) 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

1126 Eulerian Path

题意:若图是连通图,且所有结点的度均为偶数,则称为Eulerian:若有且仅有两个结点的度为奇数,则称为semi-Eulerian.现给出一个图,要我们判断其是否为Eulerian,semi-Eulerian还是not-Eulerian. 思路:在数据输入的时候计算各个节点的度:在输出各个节点的度的同时记录度为奇数的结点个数:最后判断判断图是否连通.这种题不考察什么算法,关键要我们理解题意,往往都是很简单的! 代码: #include <cstdio> #include <cstring

牛人的ACM经验 (转)

一:知识点 数据结构: 1,单,双链表及循环链表 2,树的表示与存储,二叉树(概念,遍历)二叉树的 应用(二叉排序树,判定树,博弈树,解答树等) 3,文件操作(从文本文件中读入数据并输出到文本文 件中) 4,图(基本概念,存储结构,图的运算) 数学知识 1,离散数学知识的应用(如排列组合.简单的图论,数 理逻辑) 2,数论知识 3,线性代数 4,组合代数 5,计算几何 二 算法 1,排序算法(冒抛法,插入排序,合并排序,快速排 序,堆排序) 2,查找(顺序查找,二分发) 3,回溯算法 4,递归算

《算法》第四章部分程序 part 7

? 书中第四章部分程序,包括在加上自己补充的代码,图中找欧拉环 ● 无向图中寻找欧拉环 1 package package01; 2 3 import edu.princeton.cs.algs4.StdOut; 4 import edu.princeton.cs.algs4.StdRandom; 5 import edu.princeton.cs.algs4.GraphGenerator; 6 import edu.princeton.cs.algs4.Graph; 7 import edu.

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10