我是先在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 EulerianSemi-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的更多相关文章

  1. PAT甲级 1126. Eulerian Path (25)

    1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...

  2. PAT 甲级 1126 Eulerian Path

    https://pintia.cn/problem-sets/994805342720868352/problems/994805349851185152 In graph theory, an Eu ...

  3. PAT甲级——A1126 Eulerian Path【30】

    In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...

  4. PAT 1126 Eulerian Path[欧拉路][比较]

    1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...

  5. 1126 Eulerian Path (25 分)

    1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...

  6. 【刷题-PAT】A1126 Eulerian Path (25 分)

    1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...

  7. PAT 1126 Eulerian Path

    In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...

  8. 1126. Eulerian Path (25)

    In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...

  9. PAT甲题题解-1126. Eulerian Path (25)-欧拉回路+并查集判断图的连通性

    题目已经告诉如何判断欧拉回路了,剩下的有一点要注意,可能图本身并不连通. 所以这里用并查集来判断图的联通性. #include <iostream> #include <cstdio ...

随机推荐

  1. 1 Maven简介

    一.构建(build)     清理.编译.测试.打包.部署等一系列操作. 二.maven介绍:     maven是一个强大的构建工具,能够帮助我们自动化构建过程:从清理(clean).编译(com ...

  2. Python pandas 获取Excel重复记录

    pip install pandas pip install xlrd 大量记录的时候,用EXCEL排序处理比较费劲,EXCEL程序动不动就无响应了,用pands完美解决. # We will use ...

  3. windows与mac共享文件

    实际操作环境是win10实体机与mac10.10虚拟机共享文件. 需要两步操作: 在win10中设置一个共享文件夹: 在mac中点击Finder——窗口左侧的列表——共享的——共享屏幕——输入用户名密 ...

  4. JVM client模式和Server模式

    我们把jdk安装完成后,在命名行输入java -version 不仅可以看到jdk版本相关信息,还会看到类似与 Java HotSpot(TM) 64-Bit Server VM (build 25. ...

  5. 查询所有联系人并选中显示 contentprovider

    <!-- 读取联系人记录的权限 --> <uses-permission android:name="android.permission.READ_CONTACTS&qu ...

  6. IC卡、ID卡、M1卡、射频卡的区别是什么

    IC卡.ID卡.M1卡.射频卡都是我们常见的一种智能卡,但是很多的顾客还是不清楚IC卡.ID卡.M1卡.射频卡的区别是什么,下面我们一起来看看吧. 所谓的IC卡就是集成电路卡,是继磁卡之后出现的又一种 ...

  7. codeforces B. Multitasking 解题报告

    题目链接:http://codeforces.com/problemset/problem/384/B 题目意思:给出n个数组,每个数组包括m个数字,当k = 0 时,需要把n个数组都按照从小到大的顺 ...

  8. 浅谈如何删除JSP编译后的空行

    当你在客户端用view source看JSP生成的代码时,会发现有很多空行,他们是由< %...% >后的回车换行而生成的,也就是说每一行由< %...% >包含的JSP代码到 ...

  9. web.xml中classpath 解释

    经过我在对 web.xml 的配置测试: web.xml 中classpath 所指的路径是项目工程路径下的 classes 文件夹

  10. Web前端行业的了解

    即将从事Web前端的工作的 先对即将从事的行业有个了解. Web前端发展史: 第一个网页诞生于90年代初,早期的网页除了一些小图片和毫无布局可言的标题段落,其全由文字构成.然而随着时代的进步,互联网的 ...