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 ...
随机推荐
- "errcode":40163,"errmsg":"code been used...报错,做PC微信登录时出现code been used...报错问题
这是一个坑,一个巨坑,一个恶心的坑 出现这个问题的大概意思就是微信回调了两次登录接口,code使用了两次,而在微信官方文档上写着code只能用一次,用来获取access_token,但我TM看着就糊涂 ...
- 虚拟机的ip地址为什么会发生变化
因为虚拟机在NAT模式下由Vmware8虚拟网卡提供虚拟机的IP分配,网桥模式下由Vmware1来提供IP分配.它们都相当于 一个小型的DHCP服务器,除非改动虚拟机的网络连接方式,或动了虚拟网卡服务 ...
- PLA-1
PLA(Principal Component Analysis)主成分分析: 思路1:坐标旋转 1.数据分布以及投影: 2.特征值以及特征向量 思路2: 未完待续...
- java 调用 wsdl形式的webservice 示例
import java.rmi.RemoteException; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceExc ...
- linux下使用sha256sum生成sha256校验文件,并校验其一致性
[root@localhost ]# " >test.zip 生成sha256文件校验文件 [root@localhost ]# sha256sum test.zip >test ...
- jenkins中通过execute shell启动的进程会被杀死的问题
在jenkins中配置自动更新部署项目时,如果采取用execute shell启动/关闭tomcat,会发现可以进行关闭tomcat, 但是无法启动tomcat,虽然构建会显示执行成功,但是查看进程, ...
- tomcat 和jboss区别
参考http://blog.csdn.net/sz_bdqn/article/details/6762175
- Hack You CTF 2014: NotEasyTask
exe文件,运行后闪退,peid查一下,发现是是c#写的 Reflector打开 找到Main: private static void Main(string[] args) { string ho ...
- Quick Introduction to SQL Server Profiler
Introduction to Profiler SQL Server Profiler — or just Profiler — is a tool that can help monitor al ...
- hdu-1711(kmp)
题意:给你两串数字,问你第二串数字第一次出现在第一串数字的位置,没有输出-1: 解题思路:其是就是字符串匹配,就是这里是数字匹配,把char数组改成int型就可以了: 代码: #include< ...