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 ...
随机推荐
- http请求的headers详解
关于http请求的headers详解:这里以HTTP1.1为例结合postman返回的信息 1.Server →nginx/1.15.8 A name for the server 这是post ...
- JavaScript 把字符串类型转换成日期类型
今天在写习题时,遇到些小问题,在这里把答案分享给大家,希望能帮助到大家! 一.把字符串转换成日期类型 var str = "1997-3-12"; var d = new Date ...
- React中使用百度地图API
今天我们来搞一搞如何在React中使用百度地图API好吧,最近忙的头皮发麻,感觉身体被掏空,所以很久都没来写博客了,但今天我一定要来一篇好吧 话不多说,我们直接开始好吧 特别注意:该React项目是用 ...
- Echarts简单案例
官网: http://echarts.baidu.com/index.html 文档: http://echarts.baidu.com/echarts2/doc/doc.html <html ...
- 我的Java之旅 第二课 Eclipse使用
1.项目引用的jar包管理 在Project Explorer中找到你要添加jar包的项目,右键项目名,点击Properties. 在弹出的窗体中,点击Resource中的JAVA Build Pat ...
- Linux 学习笔记之超详细基础linux命令 Part 7
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 6----------------- ...
- 浅谈Java多线程中的join方法
先上代码 新建一个Thread,代码如下: package com.thread.test; public class MyThread extends Thread { private String ...
- beta冲刺随笔集
团队成员 郑西坤 031602542 (队长) 陈俊杰 031602504 陈顺兴 031602505 张胜男 031602540 廖钰萍 031602323 雷光游 031602319 吴志鸿 03 ...
- SolrCloud集群搭建(基于zookeeper)
1. 环境准备 1.1 三台Linux机器,x64系统 1.2 jdk1.8 1.3 Solr5.5 2. 安装zookeeper集群 2.1 分别在三台机器上创建目录 mkdir /usr/hdp/ ...
- 面向对象的封装与隐藏 this
当我们创建一个对象的时候,我们可以通过‘对象.属性’的方式,对对象的属性进行赋值. 这里赋值操作要受到属性的数据类型和存储范围的制约,但是除此之外,没有其他制约条件. 但是实际问题中我们需要给这个属性 ...