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:

 /*
Data: 2019-06-01 19:33:52
Problem: PAT_A1126#Eulerian Path
AC: 56:58 题目大意:
欧拉路径可以访问图中所有的边且各边仅访问一次,欧拉回路是起点和终点相同的欧拉路径;
已知各顶点均含有偶数条边的图可构成欧拉回路,该图称作欧拉图;
若只有两个顶点含有奇数条边的图可构成欧拉路径,并且这两个结点作为欧拉路径的起点和终点;
含有欧拉路径但不含欧拉回路的图,称作半欧拉图
现在给定一个图,判断其是否为欧拉图
输入:
第一行给出,顶点数N<=500,边数M
接下来M行给出各边
输出:
第一行给出,各顶点边数
第二行给出,非欧拉图,半欧拉图,欧拉图 基本思路:
先判断含有奇数边顶点的数目,
再判断图的连通性,
若含有无奇数边顶点,且连通图,则为欧拉图
若含有两条奇数边顶点,且图连通,则为半欧啦图
否则,为非欧拉图
*/ #include<cstdio>
#include<algorithm>
using namespace std;
const int M=,INF=1e9;
int grap[M][M],vis[M],in[M],n,sum=; void DFS(int v)
{
vis[v]=;
sum++;
for(int u=; u<=n; u++)
if(vis[u]== && grap[u][v]!=INF)
DFS(u);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif fill(in,in+M,);
fill(vis,vis+M,);
fill(grap[],grap[]+M*M,INF);
int m,v1,v2,cnt=;
scanf("%d%d", &n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d", &v1,&v2);
grap[v1][v2]=;
grap[v2][v1]=;
in[v1]++;
in[v2]++;
}
for(int i=; i<=n; i++){
printf("%d%c", in[i], i==n?'\n':' ');
if(in[i]%==) cnt++;
}
DFS();
if(cnt== && sum==n)
printf("Semi-Eulerian\n");
else if(cnt== && sum==n)
printf("Eulerian\n");
else printf("Non-Eulerian\n"); return ;
}

PAT_A1126#Eulerian Path的更多相关文章

  1. Graph | Eulerian path

    In graph theory, a Eulerian trail (or Eulerian path) is a trail in a graph which visits every edge e ...

  2. PAT1126:Eulerian Path

    1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...

  3. A1126. Eulerian Path

    In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...

  4. PAT A1126 Eulerian Path (25 分)——连通图,入度

    In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...

  5. 1126 Eulerian Path (25 分)

    1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...

  6. PAT甲级 1126. Eulerian Path (25)

    1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...

  7. PAT 甲级 1126 Eulerian Path

    https://pintia.cn/problem-sets/994805342720868352/problems/994805349851185152 In graph theory, an Eu ...

  8. PAT 1126 Eulerian Path[欧拉路][比较]

    1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...

  9. PAT甲级——1126 Eulerian Path

    我是先在CSDN上发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89155050 1126 Eulerian Path ( ...

随机推荐

  1. Blue Jeans POJ 3080 寻找多个串的最长相同子串

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

  2. LINUX 内核 图 外国牛人

    http://duartes.org/gustavo/blog/ http://blog.csdn.net/drshenlei

  3. Mac下查看文件编码方式

    一句话:file -I {filename}

  4. BC ROUND 43# 03 HDU 5266

    弱啊弱啊,我用了扩展指令,然后大牛告诉我,只对VC++有用,对G++没用的..shit,三题就这样没了. 方法是使用ST在线算法,O(1)查询,然后用线段树维护..呃感觉这个好慢.看了大斌神的是用LC ...

  5. Window下UDP(socket)接和收数据案例

     配置QT的环境变量,这台电脑à属性à高级系统设置à高级à环境变量à系统变量àpathàC:\Qt\Qt5.3.0\5.3\mingw482_32\bin;C:\Qt\Qt5.3.0\Tools\ ...

  6. [ACM] ZOJ 3819 Average Score (水题)

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  7. 基于ArcGIS Flex API实现动态标绘(1.1)

    动态标绘API 1.1版本号.相较前一版本号1.0(点击进入).该版本号提供标绘符号的编辑功能. 编辑功能包含两种编辑状态:编辑控制点.对标绘符号进行旋转.八方向拉伸.平移. 编辑控制点例如以下图所看 ...

  8. Openstack能解决这些问题吗?请各位大侠一起来讨论

    1.10万规模的虚拟机,每一个虚拟机能够在不论什么一个计算节点上启动,该怎样做?计算,存储,网络都是怎么拉通与配合的? 2.用户怎样自己定义业务网络,怎样解决网络不够用的情况?正常就4096个vlan ...

  9. 第2章 安装Nodejs 2-3 Windows下安装Nodejs

    http://nodejs.org

  10. DMARC 介绍

    DMARC 是什么? DMARC 是 “Domain-based Message Authentication, Reporting & Conformance” 的缩写.它用来检查一封电邮是 ...