PAT甲级——1126 Eulerian Path
我是先在CSDN上发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89155050
1126 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:
Sample Output 1:
Eulerian
Sample Input 2:
Sample Output 2:
Semi-Eulerian
Sample Input 3:
Sample Output 3:
Non-Eulerian
题目大意:判断所给的无向图是否为欧拉图;
欧拉图的相关性质:(来源百度百科~)
1.无向连通图 G 是欧拉图,当且仅当 G 不含奇数度结点( G 的所有结点度数为偶数);
2.无向连通图G 含有欧拉通路,当且仅当 G 有零个或两个奇数度的结点;
3.有向连通图 D 是欧拉图,当且仅当该图为连通图且 D 中每个结点的入度=出度;
4.有向连通图 D 含有欧拉通路,当且仅当该图为连通图且 D 中除两个结点外,其余每个结点的入度=出度,且此两点满足 deg-(u)-deg+(v)=±1 。(起始点s的入度=出度-1,结束点t的出度=入度-1 或两个点的入度=出度);
5.一个非平凡连通图是欧拉图当且仅当它的每条边属于奇数个环;
6.如果图G是欧拉图且 H = G-uv,则 H 有奇数个 u,v-迹仅在最后访问 v ;同时,在这一序列的 u,v-迹中,不是路径的迹的条数是偶数。
思路:邻接表存图,一次深搜判断是否为连通图(定义全局变量flag,每访问一个节点就+1,DFS之后flag与节点的个数相同则为连通图);统计节点的度进而判断是否为欧拉图。
#include <iostream>
#include<vector>
using namespace std; vector<int> G[], Degree;
vector<bool> Visit;
void DFS(int vertex);
int flag = ;
int main()
{
int N, M, even = , odd = ;
scanf("%d%d", &N, &M);
Degree.resize(N + , );
Visit.resize(N + , false);
for (int i = ; i <= M; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
Degree[u]++;
Degree[v]++;
}
DFS();
for (int i = ; i <= N; i++) {
if (Degree[i] % == )
even++;
else
odd++;
printf("%d", Degree[i]);
if (i < N)
printf(" ");
}
printf("\n");
if (flag == N && even == N)
printf("Eulerian\n");
else if (flag == N && odd == )
printf("Semi-Eulerian\n");
else
printf("Non-Eulerian\n");
}
void DFS(int vertex)
{
if (Visit[vertex])
return;
Visit[vertex] = true;
flag++;
for (int i = ; i < G[vertex].size(); i++)
if (!Visit[G[vertex][i]])
DFS(G[vertex][i]); }
PAT甲级——1126 Eulerian Path的更多相关文章
- 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甲级——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 ...
- 1126 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 (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- PAT 1126 Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- 1126. Eulerian Path (25)
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT甲题题解-1126. Eulerian Path (25)-欧拉回路+并查集判断图的连通性
题目已经告诉如何判断欧拉回路了,剩下的有一点要注意,可能图本身并不连通. 所以这里用并查集来判断图的联通性. #include <iostream> #include <cstdio ...
随机推荐
- linux环境下启动tomcat7出现时间过长(已经编译完成的项目)问题解决!
已经编译完成的项目,系统启动过程中,提示: INFO: Starting Servlet Engine: Apache Tomcat/7.0.81 Sep 20, 2017 3:17:32 PM or ...
- 项目中一个普通的Java类如何获取serviceimpl实现类(二)
HbOnLineConfigServiceImpl hbOnlineService=(HbOnLineConfigServiceImpl) WebContextFactoryUtil.getBean( ...
- ubuntu下安装android模拟器genymotion【转】
本文转载自:http://www.jianshu.com/p/e6062ebb8fc9 去genymotion下载对应的安装包genymotion-2.4.0_x64.bin sudo ./genym ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- SQL.py
import sqlite3,sys def convert(value): if value.startswith('~'): return value.strip('~') if not valu ...
- 「CQOI2007」「BZOJ1260」涂色paint (区间dp
1260: [CQOI2007]涂色paint Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 2057 Solved: 1267[Submit][St ...
- [SDOI2012]任务安排
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2726 [算法] 此题与POJ1180非常相似 但是 , 此题中的t值可能为负 , 这 ...
- 【linux+C】神器 vim + 指针相关客串
前篇回顾 上篇介绍了linux下C编程基本环境配置以及相关工具使用选择. 不过10个大牛9个用vim,那么咱们就来玩vim.linux下玩c就别依靠图形界面.好吧告别Ide,命令行才是c的王道. 本篇 ...
- Linux系统启动全过程
分为两部分,第一部分是硬件本身需要加载的信息,之后才是加载Linux相关信息,因为有装有双系统的机器嘛 1.计算机加电 2.BIOS开始运行,检测硬件:cpu.内存.硬盘等 3.BIOS读取CMOS存 ...
- py-day2-sys模块、os模块、运算符、列表、字典
一.sys 模块 import sys print (sys.path)#打印环境变量 print(sys.aegv) #打印脚本的名字相对路径 print(sys.aegv)1 2 3 4 prin ...