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 ...
随机推荐
- MongoDB 数据库、集合创建删除与文档插入
本文章主要介绍mongodb的基本命令,前提条件,你的本地已经安装了mongo. 一.基本命令使用(主要是创建,增删改.) 0.mongoDb统计信息 获得关于MongoDB的服务器统计,需要在Mon ...
- UIBezierPath(转)
@import url(/css/cuteeditor.css); @import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com ...
- poj2586 Y2K Accounting Bug —— 枚举
链接:http://poj.org/problem?id=2586 题意:大意是一个公司在12个月中,或固定盈余s,或固定亏损d.但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(和 ...
- v-for指令用法二
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- ZOJ3496 Assignment
传送门 这题也是真恶心-- 题目大意是俩公司要运货,每条路有容量上限.然后B公司手里有p个--(技能点?)如果在一条路上放了x个技能点,这条路经过了y个货物,那么B公司就会收x*y的钱.现在要求的是, ...
- Ubuntu16.04 安装cuda9.0 cudnn 7.0.5
参考网址:https://blog.csdn.net/zhuangwu116/article/details/81063234 (1)下载安装文件: 下载cuda9.0 runfile 文件 下载地址 ...
- HTTP上传大文件要考虑的问题
1.大文件上传服务器内存占用 一般WEB开发框架如SpringMVC,在基于Web容器如Tomcat处理HTTP请求时,都倾向于采用职责链流水线式的处理机制.HTTP请求被封装为一个可解析对象放在内存 ...
- centos7 安装 python3.5
centos7 安装 python3.5 一. python虚拟环境virtualenv VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前 ...
- 五 pyJWT使用
PyJWT是一个Python库,用来编码/解码JWT(JSON Web Token)的. 1:安装PyJWT 2: 直接上代码了: import datetime, jwt, time from a ...
- 重载和const形参的学习心得
在C++ primer 5th中,第6章的练习6.39(a)中,关于重载和const形参的学习心得. 练习6.39:说明在下面的每组声明中第二条声明语句是何含义.如果有非法的声明,请指出来. (a) ...