A1126. 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 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<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int G[][] = {,}, visit[] = {};
int N, M;
void DFS(int vt){
visit[vt] = ;
for(int i = ; i <= N; i++){
if(G[vt][i] != && visit[i] == )
DFS(i);
}
}
int main(){
scanf("%d%d", &N, &M);
for(int i = ; i < M; i++){
int v1, v2;
scanf("%d%d", &v1, &v2);
G[v1][v2] = G[v2][v1] = ;
}
int odds = , even = , cnt = ;
for(int i = ; i <= N; i++){
int sum = ;
for(int j = ; j <= N; j++){
sum += G[i][j];
}
if(i == N)
printf("%d\n", sum);
else printf("%d ", sum);
if(sum % == ){
odds = ;
}else{
even = ;
cnt++;
}
}
DFS();
for(int i = ; i <= N; i++)
if(visit[i] == ){
printf("Non-Eulerian");
return ;
}
if(even == ){
printf("Eulerian");
}else if(cnt == ){
printf("Semi-Eulerian");
}else printf("Non-Eulerian");
cin >> N;
return ; }
总结:
1、容易被忽略的:Semi-Eulerian和 Eulerian都是建立在连通图的基础上。最开始忽略了连通图的条件,结果有一个测试点过不去。应该先判断是否连通,不连通为 Non-Eulerian。
A1126. Eulerian Path的更多相关文章
- PAT A1126 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】A1126 Eulerian Path (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- PAT_A1126#Eulerian Path
Source: PAT A1126 Eulerian Path (25 分) Description: In graph theory, an Eulerian path is a path in a ...
- 1126 Eulerian Path (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- 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 ...
- 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 ...
随机推荐
- PMP三点
三点估算:悲观36天,可能21天,乐观6天.在16至26天内完成的概率是多少?这个算法是PERT估算最终估算结果=(悲观工期+乐观工期+4×最可能工期)/6=(36+6++4*21)/6=21标准差= ...
- C++多态(静多态和动多态)
如今的C++已经是个多重泛型编程语言(multiparadigm programming lauguage),一个同时支持过程形式(procedural).面向对象形式(object-oriented ...
- Sublime Text3配置
{ "default_encoding": "UTF-8", "font_size": 16.0, "tab_size" ...
- Django--CRM--菜单展示, 删除合并, 权限展示
一 . 菜单展示 二 . 合并删除 我们可以把所有的删除都合并成一个函数这样就会减少很多的代码. 思路: 在url里面需要传两个参数,一个是要删的id 一个是名字 三 .权限展示 我们要实现两个功能 ...
- k8s容器的资源限制
1.k8s支持内存和cpu的限制 requests:容器运行需求,最低保障limits:限制,硬限制(资源上限) CPU: 1颗逻辑CPU(1核CPU=4个逻辑CPU) 1物理核=1000个微核(mi ...
- Java线程的5种状态及切换(透彻讲解)-京东面试
一.Thread的几个重要方法: 我们先了解一下Thread的几个重要方法. a.start()方法,开始执行该线程:b.stop()方法,强制结束该线程执行:c.join方法,等待该线程结束.d.s ...
- Java之指定Junit测试方法的执行顺序举例
问题描述: 大家都知道使用JUnit进行测试的时候,方法的执行顺序不是按照编写的先后顺序执行的,那么如何控制Junit的执行顺序呢? 解决方法: 在测试类上加 @FixMethodOrder 注解即可 ...
- Attention Model
参考1: https://blog.csdn.net/malefactor/article/details/50550211 attention部分实现: https://blog.csdn.net ...
- hdu-2717(基础搜索bfs)
题意:给你n和k,问你n最少花费多少代价能得到k: 有两种变换:1.n++或者n--: 2.n=n*2: 两种代价每次的花费都是1: 思路:一维的bfs,每次入队三个点,一个是n+1,一个是n-1,一 ...
- NIKKEI Programming Contest 2019 翻车记
A:签到. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> ...