PAT甲级 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 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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int inf=0x3f3f3f3f; int cnt[10005];
int pre[10005];
void init()
{
for(int i=0; i<10005; i++)
pre[i]=i;
} int fin(int x)
{
return x==pre[x]?x:pre[x]=fin(pre[x]);
} int main()
{
int m,n,u,v;
scanf("%d%d",&m,&n);
memset(cnt,0,sizeof cnt);
init();
for(int i=0; i<n; i++)
{
scanf("%d%d",&u,&v);
int a=fin(u);
int b=fin(v);
if(a!=b)
pre[a]=b;
cnt[u]++,cnt[v]++;
}
int ans=0;
int q=0;
int flag=0;
for(int i=1; i<=m; i++)
{
if(cnt[i]%2) ans++;
if(pre[i]==i) flag++;
if(q++)
printf(" ");
printf("%d",cnt[i]);
}
printf("\n");
if(flag==1)
printf("%s\n",ans==0?"Eulerian":(ans<=2)?"Semi-Eulerian":"Non-Eulerian");
else
printf("Non-Eulerian\n");
return 0;
}
PAT甲级 1126. Eulerian Path (25)的更多相关文章
- PAT甲级——1126 Eulerian Path
我是先在CSDN上发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89155050 1126 Eulerian Path ( ...
- PAT 甲级 1126 Eulerian Path
https://pintia.cn/problem-sets/994805342720868352/problems/994805349851185152 In graph theory, an Eu ...
- 1126. Eulerian Path (25)
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT甲级——A1126 Eulerian Path【30】
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT甲题题解-1126. Eulerian Path (25)-欧拉回路+并查集判断图的连通性
题目已经告诉如何判断欧拉回路了,剩下的有一点要注意,可能图本身并不连通. 所以这里用并查集来判断图的联通性. #include <iostream> #include <cstdio ...
- 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[欧拉路][比较]
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- 【刷题-PAT】A1126 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
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
随机推荐
- Attribute Syntax
Attribute Syntax This section describes the syntax with which __attribute__ may be used, and the con ...
- Oracle_PL/SQL(2) 过程控制
0.检索单行数据0.1使用标量变量接受数据例1: 7788declare v_ename emp.ename%type; v_sal emp.sal%type;begin select ename,s ...
- 3G - 汉字统计
统计给定文本文件中汉字的个数. Input 输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本. Output 对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行. [Hint ...
- Hibbernate详解一
这里先做一个简单的入门,后面有详解 记住图解原理: 这里只是没有整合spring等项目使用的hibernate的使用详解. 一.Hibernate简介 1.Hibernate在开发中所处的位置 2.O ...
- memcached 一致性hash原理
memcache 是一个分布式的缓存系统,但是本身没有提供集群功能,在大型应用的情况下容易成为瓶颈.但是客户端这个时候可以自由扩展,分两阶段实现.第一阶段:key 要先根据一定的算法映射到一台memc ...
- PID参数调节口诀
参数整定找最佳, 从小到大顺序查. 先是比例后积分, 最后再把微分加. 曲线振荡很频繁, 比例度盘要放大. 曲线漂浮绕大弯, 比例度盘往小扳. 曲线偏离回复慢, 积分时间往下降. 曲线波动周期长, 积 ...
- vim自动添加tags、cscope
每次打开一个工程都需要重新添加tags.cscope,很不方便,网上找了一下. 将其添加到 ~/.vimrc 中之后,打开 vim 就会自动搜索当前路径下的tags.cscope添加进去. 自动添加 ...
- linux学习第二天 (Linux就该这么学)
2018年11月10日,今天是学习的第二天 今天学习了安装vmware workstation12的安装及怎么安装 redhat7系统,在新建虚拟机时注意要选择“稍后安装操作系统”要vmwark wo ...
- apt install yum失败
解决办法:sudo apt-get update
- 配置nginx1.8支持thinkPHP3.2 pathinfo模式
nginx 下conf/nginx.conf 或者自己的vhosts更改以前的参数 location / { root html; index index.html index.htm inde ...