我是先在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.JavaScript:写入 HTML 输出

    ①JavaScript 是可插入HTML页面的编程代码 ②JavaScript插入HTML页面后,可有所有的现代浏览器执行 ※提示:您只能在 HTML 输出中使用 document.write.如果您 ...

  2. 20170228 交货单过账增强 MV50AFZ1

    MV50AFZ1   这个程序里面找个FORM 用户出口, FORM USEREXIT_SAVE_DOCUMENT_PREPARE.   用户出口如下: 例:需求: 开发要求:制作交货单的人员,需要同 ...

  3. 高精度乘法(FFT)

    学会了FFT之后感觉自己征服了世界! 当然是幻觉... 不过FFT还是很有用的,在优化大规模的动规问题的时候有极大效果. 一般比较凶残的计数动规题都需要FFT(n<=1e9). 下面是高精度乘法 ...

  4. 英语发音规则---gh

    英语发音规则---gh 一.总结 一句话总结:gh字母组合的读音在中学英语课本中归纳起来主要有"发音"和"不发音"两种情况. gh字词首是发/g/,因为需要开头 ...

  5. Normalize.css 与传统的 CSS Reset 有哪些区别?

    CSS Reset 是革命党,CSS Reset 里最激进那一派提倡不管你小子有用没用,通通给我脱了那身衣服,凭什么你 body 出生就穿一圈 margin,凭什么你姓 h 的比别人吃得胖,凭什么你 ...

  6. web.xml 有什么用?

    1.每个javaEE工程中都有web.xml文件,那么它的作用是什么呢?它是每个web.xml工程都必须的吗? 一个web中可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须 ...

  7. Opencv— — kaleidoscope Filter

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  8. MongoDB分析工具之三:db.currentOp()

    db.currentOp() db.currentOp是个好东西,顾名思义,就是当前的操作.在mongodb中可以查看当前数据库上此刻的操作语句信息,包括insert/query/update/rem ...

  9. [置顶] 谈EXPORT_SYMBOL使用

    转自:http://blog.csdn.net/macrossdzh/article/details/4601648 EXPORT_SYMBOL只出现在2.6内核中,在2.4内核默认的非static ...

  10. PHP开发api接口 -- 安全验证 生成签名

    转载博客 ————. http://blog.csdn.net/li741350149/article/details/62887524 REST模式中HTTP请求方法(GET,POST,PUT,DE ...