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 ...
随机推荐
- ubuntu上mongodb的安装
Ubuntu上安装MongoDB的完全步骤以及注意事项 本文我们详细介绍了Ubuntu上安装MongoDB的全部过程,希望本次的介绍能够对您有所帮助. AD: 2013大数据全球技术峰会课程PPT下载 ...
- Vs2015 c# 诊断工具查看程序的占用情况
windbg用着还不熟悉,dottrace 还要版权,着急查看程序的cpu 的使用情况,因为程序开启之后占用处理器资源较大,问题在哪里呢,于是点开了vs2015自带的诊断工具,以前偶尔打开过,没发现 ...
- Python.Flask.0
Resource List of Flask 1. 吐槽 Python Web 框架 Flask https://blog.tonyseek.com/post/discuss-about-flask- ...
- geoserver 发布无坐标png
1.geoserver 安装一些插件的时候,直接去http://docs.geoserver.org/ 官网,选择自己geoserver对应的版本的插件就可以了. 2.geoserver 发布自身带有 ...
- iOS通过URL构建UIImage
很多时候我们只能得到一个URL,然后需要构建一个UIImage. 通常情况下,我们一般都是通过SDWebImage来直接构建UIImageVIew的image,如何用URL直接构建UIImage呢? ...
- UI设计师需要熟记的45个快捷键Windows、Mac
大家都知道PS快捷键很多,其实没必要都记住,今天为大家整理了45个比较实用的,别忘了收藏. 图层 填充图层 MAC: Alt+Backspace (前景) or Cmd+Backspace (背景) ...
- How to ignore files and directories in subversion?
Step 1 Copy the files and directories to other place. Step 2 Delete the files and directories. Step ...
- 论坛:设计实体-->分析功能-->实现功能 之 《分析功能》
其中 管理文章 的功能没有做,以下做的设计 浏览与参与 功能的步骤 分析功能 5个功能. 7个请求. 实现功能 Action, 7个方法 Service Dao Jsp For ...
- DataTable xml 互相转换
//测试方法 public static DataTable Test() { string savePath = System.AppDomain.CurrentDomain.BaseDirecto ...
- Eigen中的map
Map类用于通过C++中普通的连续指针或者数组 (raw C/C++ arrays)来构造Eigen里的Matrix类,这就好比Eigen里的Matrix类的数据和raw C++array 共享了一片 ...