PAT_A1126#Eulerian Path
Source:
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
Eulerian
,Semi-Eulerian
, orNon-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的更多相关文章
- Graph | Eulerian path
In graph theory, a Eulerian trail (or Eulerian path) is a trail in a graph which visits every edge e ...
- PAT1126:Eulerian Path
1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...
- A1126. Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT A1126 Eulerian Path (25 分)——连通图,入度
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- 1126 Eulerian Path (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- PAT甲级 1126. Eulerian Path (25)
1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...
- PAT 甲级 1126 Eulerian Path
https://pintia.cn/problem-sets/994805342720868352/problems/994805349851185152 In graph theory, an Eu ...
- PAT 1126 Eulerian Path[欧拉路][比较]
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- PAT甲级——1126 Eulerian Path
我是先在CSDN上发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89155050 1126 Eulerian Path ( ...
随机推荐
- 洛谷 P1491 集合位置
P1491 集合位置 题目描述 每次有大的活动,大家都要在一起“聚一聚”,不管是去好乐迪,还是避风塘,或者汤姆熊,大家都要玩的痛快.还记得心语和花儿在跳舞机上的激情与释放,还记得草草的投篮技艺是如此的 ...
- 改进MySQL Order By Rand()的低效率
Author:flymorn Source:飘易Categories:PHP编程 PostTime:2011-1-14 15:35:07 正 文: 最近由于需要研究了一下MYSQL的随机抽取实现方法. ...
- VMware镜像文件下载
VMware镜像文件下载 http://blog.sina.com.cn/s/blog_517c21c00102x5ja.html 貌似Centos 6不能下载啊: 其他的没有测试:
- 飘逸的python - 实现一个极简的优先队列
一个队列至少满足2个方法,put和get. 借助最小堆来实现. 这里按"值越大优先级越高"的顺序. #coding=utf-8 from heapq import heappush ...
- Python基础--正則表達式基本的语法以及re模块
正则是个非常牛逼的东西,python中当然也不会缺少. 所以今天的Python就跟大家一起讨论一下python中的re模块. re模块包括对正則表達式的支持. 什么是正则: 正則表達式是能够匹配文本片 ...
- Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心
C. Palindrome Transformation Nam is playing with a string on his computer. The string consists o ...
- com关于引用计数
实现引用计数并不难,但在什么层次上进行引用计数呢? 依照com规范,一个com组件能够实现多个com对象.而且每一个com对象又能够支持多个com接口,这样的层次结构为我们实现引用计数提供了多种选择方 ...
- 阿里云 Docker-registry 搭建
阿里云 仓库地址: https://cr.console.aliyun.com/cn-hangzhou/instances/images
- nyoj--496--巡回赛(拓扑排序)
巡回赛 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 世界拳击协会(WBA)是历史最悠久的世界性拳击组织,孕育了众多的世界冠军,尤其是重量级,几乎造就了大家耳熟能详的所 ...
- [Apple开发者帐户帮助]三、创建证书(8)撤销证书
您可以根据证书类型和角色撤消证书.有关详细信息,请转到撤消权限. 要了解撤销证书时会发生什么,请转到Apple Developer支持中的证书. 所需角色:帐户持有人或管理员. 在“ 证书”,“标识符 ...