PAT A1126 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 <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=;
int g[maxn][maxn];
int deg[maxn];
int n,m;
int num=;
bool vis[maxn]={false};
void dfs(int st){
if(vis[st]==false){
vis[st]=true;
num++;
for(int i=;i<=n;i++){
if(vis[i]==false && g[st][i]==){
dfs(i);
}
}
}
}
int main(){
scanf("%d %d",&n,&m);
for(int i=;i<m;i++){
int c1,c2;
scanf("%d %d",&c1,&c2);
g[c1][c2]=g[c2][c1]=;
deg[c1]++;
deg[c2]++;
}
dfs();
int cnt=;
for(int i=;i<=n;i++){
printf("%d",deg[i]);
printf("%s",i==n?"\n":" ");
if(deg[i]%==)cnt++;
}
//printf("%d %d\n",num,cnt);
if(num==n && cnt==){
printf("Eulerian\n");
}
else if(num==n && cnt==){
printf("Semi-Eulerian\n");
}
else{
printf("Non-Eulerian\n");
}
}
注意点:直接根据题目字面意思实现就好了。首先看是不是连通图,再看入度为奇数的有几个,没有就是Eulerian,有2个就是semi,其余为non
PAT A1126 Eulerian Path (25 分)——连通图,入度的更多相关文章
- PAT甲级 1126. Eulerian Path (25)
1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- 【刷题-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【30】
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT 1126 Eulerian Path[欧拉路][比较]
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- A1126. Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT 1126 Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
随机推荐
- 【开发工具之eclipse】7、eclipse代码自动提示,eclipse设置代码自动提示
首先打开Eclipse开发软件,然后在工具栏的[Window],点击后找到弹出列表中的[Preferences]选项,然后点击这个选项 现在弹出的窗口就是JAVA开发人员常见的设置窗口了,我们依次 ...
- Java面试总结(集合、spring)
Java 集合框架简介 Java Collections Framework,最开始也是一个开源框架,后来被收录到JDK中 所谓的集合,就是能存放多个数据元素的容器,在Java中原生的容器是数组 数组 ...
- 通过kubernetes构建ela服务
一.kubernetes 通过yaml 创建pod与service apiVersion: extensions/v1beta1 kind: Deployment metadata: name: el ...
- HotSpot 虚拟机对象揭秘【转载】
对象的创建 Java 对象的创建过程, ①类加载检查: 虚拟机遇到一条 new 指令时,首先将去检查这个指令的参数是否能在常量池中定位到这个类的符号引用,并且检查这个符号引用代表的类是否已被加载过.解 ...
- Python全栈学习_day006作业
Day6作业及默写 ,使用循环打印以下效果: : * ** *** **** ***** : ***** **** *** ** * : * *** ***** ******* ********* . ...
- C#委托之我见
委托的使用方式很简单,了解一下基本语法就可以开撸了.但是使用委托的真正难题是不知道应用场景,就像习得了一门新功夫,但是却找不到任何施展拳脚的地方.这个难题一直困然着我,直到最近仿佛有所领悟,所以赶紧记 ...
- 对JavaScript中闭包的理解
在前端开发中闭包是一个很重要的知识点,是面试中一定会被问到的内容.之前我对闭包的理解主要是"通过闭包可以在函数外部能访问到函数内部的变量",对闭包运用的也很少,甚至自己写过闭包自己 ...
- OneAPM大讲堂 | 谁更快?JavaScript 框架性能评测
文章系国内领先的 ITOM 管理平台供应商 OneAPM 编译呈现. 网页性能是一个丰富且又复杂的话题.在本帖中,我们会将讨论的范围局限在前端 JavaScript 框架上,探究相对于另外一种框架而言 ...
- log4.net 配置 - 自定义过滤器按LoggerName过滤日志
自定义过滤器按LoggerName过滤日志,本来想使用 PropertyFilter 来实现,后来研究发现一直不能成功,源代码debug了一下获取一直为null,时间关系只好用 StringMatch ...
- Linux 小知识翻译 - 「Unix」和「兼容Unix的OS」
经常有人会问「Linux和Unix有什么区别?」,「Linux就是Unix吗?」. 回答一般都是「Linux是仿照Unix而开发的OS」,「Linux和Unix相似但不是一种OS」之类的. 关于「Li ...