我是先在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. Page Objects

    Page Objects 原文地址:https://github.com/SeleniumHQ/selenium/wiki/PageObjects Within your web app's UI t ...

  2. Oracle rac 配置Weblogic数据源时 实例名及URL的选择

    Oracle 10G 是 RAC 的,即有两个节点.两个节点 IP及实例名分别为:10.1.43.11 stnic110.1.43.21 stnic2配置数据源时 一直使用的是第一个 URL 及 实例 ...

  3. Android Touch事件分发

    跟touch事件相关的3个方法: public boolean dispatchTouchEvent(MotionEvent ev); //用来分派event public boolean onInt ...

  4. NSDate的具体使用(转载)

    NSDate的具体使用 时间与日期处理 主要有以下类: NSDate -- 表示一个绝对的时间点 NSTimeZone -- 时区信息 NSLocale -- 本地化信息 NSDateComponen ...

  5. govendor

    cd  到工程目录. govendor init : 初始化 govendor fetch : 拉取包 go 1.6以后编译go代码会优先从vendor目录先寻找依赖包: controllers\ar ...

  6. postNotificationName 消息传递详解

      1.定义消息创建的关联值 也就是找到方法的标志 NSString *const GameToIPhoneNotification = @"GameToIPhoneNotification ...

  7. Jmeter参数化_CSV Data Set Config

    1. 在用函数助手进行参数化的时候遇到一个问题,每个线程组每次循环的时候读取的值都是一样的,为了解决这个问题,将函数助手替换为CSV_Data_Set_Config. 2. 添加配置元件csv dat ...

  8. Tensorflow深度学习之十二:基础图像处理之二

    Tensorflow深度学习之十二:基础图像处理之二 from:https://blog.csdn.net/davincil/article/details/76598474   首先放出原始图像: ...

  9. python 文件与文件夹常见操作以及os.walk的用法

    文件操作: In [34]: import os In [35]: os.rename("hello[复件].py","hello111.py")       ...

  10. js的浅克隆和深克隆

    谈一谈个人对js浅克隆和深克隆的区别. 之前也看到很多博客在写,当然也有写的非常好的,但是个人觉得既然要分享就不要写的太深奥,尽量以简单易懂为主. 浅克隆其实就是 对象A = 对象B:如果改变了对象B ...