PAT1126:Eulerian Path
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 思路
判断一个图:
1)是不是欧拉图。
2)半欧拉图。
3)二者都不是。 1.邻接表存储图,dfs先确定连通性。不连通则为3)
2.在图连通的基础上确定是欧拉图还是半欧拉图。点的度数全为偶数为欧拉图,恰好有两个点度数为奇数是半欧拉图,其余情况二者皆不是。 代码
#include<vector>
#include<iostream>
using namespace std;
int cnt = 0;
vector<bool> isvisit(501,false); void dfs(int root,const vector<vector<int>>& graph)
{
isvisit[root] = true;
cnt++;
for(int i = 0;i < graph[root].size();i++)
{
if(!isvisit[graph[root][i]])
dfs(graph[root][i],graph);
}
} int main()
{
int N,M;
while(cin >> N >> M)
{
vector<vector<int>> vertices(N+1);
for(int i = 0;i < M;i++)
{
int a,b;
cin >> a >> b;
vertices[a].push_back(b);
vertices[b].push_back(a);
}
int countOdds = 0;
for(int i = 1;i <= N;i++)
{
if(i == 1)
cout << vertices[i].size();
else
cout << " " << vertices[i].size();
if(vertices[i].size() % 2 != 0 )
{
countOdds++;
}
}
cout << endl;
dfs(1,vertices); if(cnt == N && countOdds == 0)
cout << "Eulerian" << endl;
else if(cnt == N && countOdds == 2)
cout << "Semi-Eulerian" << endl;
else
cout << "Non-Eulerian" << endl;
}
}
PAT1126:Eulerian Path的更多相关文章
- Graph | Eulerian path
In graph theory, a Eulerian trail (or Eulerian path) is a trail in a graph which visits every edge e ...
- A1126. Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT A1126 Eulerian Path (25 分)——连通图,入度
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- 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 (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 ...
- PAT 1126 Eulerian Path[欧拉路][比较]
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- PAT甲级——1126 Eulerian Path
我是先在CSDN上发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89155050 1126 Eulerian Path ( ...
- PAT 1126 Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
随机推荐
- Cocos2D中的Framerate状态
对于额外绘制调试物理引擎的支持,Cocos2D同样可以绘制概述计数器,尤其是帧速率(framerate)显示. 为了启用这些概述计数器标签,你只需添加如下一行代码,比如说在AppDelegate.m里 ...
- 怎样使用projectproperty sheet(.vsprops)来管理工程
怎样使用projectproperty sheet(.vsprops)来管理工程 IDE:VS2005 前言 Project Property Sheet的意思是项目属性表,在大型项目中基本上都会使用 ...
- Mplayer 的编译
由于项目要用到mplayer,所以要对mplayer进行编译,首先我的平台如下: 系统:windows xp2 安装好mingw+msys,目录为c:/mingw,其中装的gcc是4.3.2版本的. ...
- 嵌入式C语言查表法的项目应用
嵌入式C实战项目开发技巧:如果对一个有规律的数组表进行位移操作 就像下面的这个表 之前写过上面这个标题的一篇文章,讲的是以位移的方式去遍历表中的数据,效率非常高,但是,如果要实现一个乱序的流水灯或者跑 ...
- 添加启动游戏过渡场景Default Splash Scene(Unity3D开发之十三)
添加启动游戏过渡场景Default Splash Scene(Unity3D开发之十三) 猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blo ...
- SpriteBuilder全屏弹出菜单的特殊效果
但是等一下!这里可以有更多.对于全屏的弹出菜单,你可以在显示弹出全屏菜单时朦胧化背景的level视图. 通过修改SpriteBuilder中的color节点的Color属性(比如修改为black)和O ...
- C语言实现快速翻转数组的顺序
#include <stdio.h> void Reverse(int *p , int size) { int i , tmp; for(i = 0 ; i < size/2 ; ...
- "C#":MySql批量数量导入
现在对数据库(以MySql为例)的操作大多会封装成一个类,如下例所示: namespace TESTDATABASE { public enum DBStatusCode { ALL_OK, MySq ...
- Linux - 停机常用的anacron
什么是 anacron anacron 并不是用来取代 crontab 的,anacron 存在的目的就在於我们上头提到的,在处理非 24 小时一直启动的 Linux 系统的 crontab 的运行! ...
- 存储引擎-Buffered tree
Buffered-tree 也称为COLA,即cache-oblivious,可以不需要知道具体内存大小和一个块的大小,使用一套逻辑进行处理,因此内存大小可知,内存可能被临时占用去做其它事情. Buf ...