PAT甲级——A1126 Eulerian Path【30】
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:
7 12
5 7
1 2
1 3
2 3
2 4
3 4
5 2
7 6
6 3
4 5
6 4
5 6
Sample Output 1:
2 4 4 4 4 4 2
Eulerian
Sample Input 2:
6 10
1 2
1 3
2 3
2 4
3 4
5 2
6 3
4 5
6 4
5 6
Sample Output 2:
2 4 4 4 3 3
Semi-Eulerian
Sample Input 3:
5 8
1 2
2 5
5 4
4 1
1 3
3 2
3 4
5 3
Sample Output 3:
3 3 4 3 3
Non-Eulerian
#include <iostream>
using namespace std;
int path[] = { }, graph[][];
int n, m, a, b, odd = , num = ;
bool visit[] = { false };
void DFS(int s)
{
visit[s] = true;
num++;
for (int i = ; i <= n; ++i)
if (graph[s][i] == && visit[i] == false)
DFS(i);
}
int main()
{
cin >> n >> m;
while (m--)
{
cin >> a >> b;
path[a]++;
path[b]++;
graph[a][b] = graph[b][a] = ;
}
for (int i = ; i <= n; ++i)
{
if (path[i] % == )
odd++;
cout << (i == ? "" : " ") << path[i];
}
DFS();//判断是不是连通图
if (num == n && odd == )
cout << endl << "Eulerian" << endl;
else if (num == n && odd == )
cout << endl << "Semi-Eulerian" << endl;
else
cout << endl << "Non-Eulerian" << endl;
return ;
}
PAT甲级——A1126 Eulerian Path【30】的更多相关文章
- 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甲级——1126 Eulerian Path
我是先在CSDN上发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89155050 1126 Eulerian Path ( ...
- 【刷题-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 A1126 Eulerian Path (25 分)——连通图,入度
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- A1126. Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)
题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...
- PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, to ...
- PAT甲级——A1155 HeapPaths【30】
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
随机推荐
- vue $emit 子传父
我们使用子组件传递值给父组件使用 $emit 代码 <!DOCTYPE html> <html lang="en"> <head> <me ...
- spark-sql中的分析函数的使用
分析函数的应用场景: (1)用于分组后组内排序 (2)指定计算范围 (3)Top N (4)累加计算 (5)层次计算 分析函数的一般语法: 分析函数的语法结构一般是: 分析函数名(参数) over ...
- java zxing 生成条形码和二维吗
依赖 <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</art ...
- SQL优化之慢查询和explain以及性能分析
性能优化的思路 首先需要使用慢查询功能,去获取所有查询时间比较长的SQL语句 使用explain去查看该sql的执行计划 使用show profile去查看该sql执行时的性能问题 MySQL性能优化 ...
- 《DNS攻击防范科普系列3》 -如何保障 DNS 操作安全
引言 前两讲我们介绍了 DNS 相关的攻击类型,以及针对 DDoS 攻击的防范措施.这些都是更底层的知识,有同学就来问能否讲讲和我们的日常操作相关的知识点,今天我们就来说说和我们日常 DNS 操作相关 ...
- [学习笔记] $FWT$
\(FWT\)--快速沃尔什变化学习笔记 知识点 \(FWT\)就是求两个多项式的位运算卷积.类比\(FFT\),\(FFT\)大多数求的卷积形式为\(c_n=\sum\limits_{i+j=n}a ...
- FFT的应用
FFT的应用 --讲稿 概述 FFT的模板很简单,大家都会背,于是出题的空间就在于建模了.FFT的题目难在建模,往往需要将问题抽象出来,经过一系列转化后得到乘积式的和,再赋予式子各个项的系数一定的意义 ...
- 顺时针打印矩阵元素(python实现)
我觉得我的算法比较简单易懂,比网上的那些简单些.至于时间复杂度就没有比较了. 算法思想:从最外层向内层遍历矩阵 # my algorithmimport math def print_matrix(m ...
- python语法基础(类)
一.什么是类? 类是具有相同属性的一类事物 类还有功能和属性,属性就是这类事物的特征,而功能就是它能做什么,也是就是方法或者函数. 在python中类用关键词class来声明 二.类的声明 类的声明方 ...
- 2019-8-31-C#-通过编程的方法在桌面创建回收站快捷方式
title author date CreateTime categories C# 通过编程的方法在桌面创建回收站快捷方式 lindexi 2019-08-31 16:55:58 +0800 201 ...