【PAT】【Advanced Level】1126. Eulerian Path (25)

2024-06-17 04:18

本文主要是介绍【PAT】【Advanced Level】1126. Eulerian Path (25),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1126. Eulerian Path (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
原题链接:

https://www.patest.cn/contests/pat-a-practise/1126

思路:

统计节点的度数

判断是否是连通图

判断是否有欧拉回路

CODE:

#include<iostream>
#include<vector>
#include<cstring>
#define N 510
using namespace std;
vector<int>  ed[N];
bool flag[N];
void dfs(int n)
{flag[n]=1;for (int i=0;i<ed[n].size();i++){if (flag[ed[n][i]]==0){dfs(ed[n][i]);}}return;
}
int main()
{int n,m;cin>>n>>m;for (int i=0;i<m;i++){int a,b;cin>>a>>b;	ed[a].push_back(b);ed[b].push_back(a);}int sum=0;for (int i=1;i<=n;i++){if (i!=1) cout<<" ";int ss=ed[i].size();cout<<ss;if (ss%2==1) sum++;}int con=0;memset(flag,0,sizeof(flag));for (int i=1;i<=n;i++){if (flag[i]==0){con++;dfs(i);}}if (con>1){cout<<endl<<"Non-Eulerian"<<endl;}else{if (sum==0) {cout<<endl<<"Eulerian"<<endl;}else if (sum==2){cout<<endl<<"Semi-Eulerian"<<endl;}else{cout<<endl<<"Non-Eulerian"<<endl;}}return 0;
}




这篇关于【PAT】【Advanced Level】1126. Eulerian Path (25)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068457

相关文章

PAT-1039 到底买不买(20)(字符串的使用)

题目描述 小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以

PAT-1028

题目描述 某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。 输入描述: 输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个

PS系统教程25

介绍软件 BR(bridge) PS 配套软件,方便素材整理、管理素材 作用:起到桥梁作用 注意:PS和BR尽量保持版本一致 下载和安装可通过CSDN社区搜索,有免费安装指导。 安装之后,我们打开照片只需双击照片,就自动在Ps软件中打开。 前提:电脑上有PS软件 三种预览格式 全屏预览 评星级 直接按数字键就可以 方向键可以更换图片 esc退出 幻灯片放

Java compiler level does not match the version of the installed Java project facet. map解决方法

右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面。 在页面中的“Java”下拉列表中,选择相应版本就OK了。

【团队成长】2024-25周周报-业务介绍内容创作

大家好!我们是IndustryOR 团队,致力于分享业界落地的算法技术。欢迎关注微信公众号/知乎/CSDN【运筹匠心】 。 记录人:张哲铭,某互联网大厂算法专家 【团队成长/个人成长】系列的推文会以 【工作周报】 的方式记录IndustryOR团队及其成员的成长过程,请大家一起见证和参与我们团队从0-1-N的发展过程。 记录人顺序:张哲铭-向杜兵-高欣甜-黄世鸿-许佳鸣

Android自定义系列——9.Path详细用法

rXxx方法 rXxx方法的坐标使用的是相对位置(基于当前点的位移),而之前方法的坐标是绝对位置(基于当前坐标系的坐标)。 Path path = new Path();path.moveTo(100,100);path.lineTo(100,200);canvas.drawPath(path,mDeafultPaint); 在这个例子中,先移动点到坐标(100,100)处,之后再连接

Android自定义系列——8.Path之贝塞尔曲线

贝塞尔曲线能干什么 贝塞尔曲线作用十分广泛,简单举几个的栗子: QQ小红点拖拽效果一些炫酷的下拉刷新控件阅读软件的翻书效果一些平滑的折线图的制作很多炫酷的动画效果 理解贝塞尔曲线的原理 一阶曲线原理: 一阶曲线是没有控制点的,仅有两个数据点(A 和 B),最终动态过程如下: (本文中贝塞尔曲线相关的动态演示图片来自维基百科)。一阶曲线其实就是前面讲解过的lineTo。 二阶曲线

Android自定义系列——7.Path之基本操作

Path常用方法表 为了兼容性(偷懒) 本表格中去除了部分API21(即安卓版本5.0)以上才添加的方法。 作用相关方法备注移动起点moveTo移动下一次操作的起点位置设置终点setLastPoint重置当前path中最后一个点位置,如果在绘制之前调用,效果和moveTo相同连接直线lineTo添加上一个点到当前点之间的直线到Path闭合路径close连接第一个点连接到最后一个点,形成一个闭合

昇思25天学习打卡营第5天|网络构建

一、简介: 神经网络模型是由神经网络层和Tensor操作构成的,mindspore.nn提供了常见神经网络层的实现,在MindSpore中,Cell类是构建所有网络的基类(这个类和pytorch中的modul类是一样的作用),也是网络的基本单元。一个神经网络模型表示为一个Cell,它由不同的子Cell构成。使用这样的嵌套结构,可以简单地使用面向对象编程的思维,对神经网络结构进行构建和管理。

从PATH说起的shell命令行替换

许久之前,师弟问了我一个问题,为什么shell中添加环境变量的写法是下面这种方式 PATH=~/.lib:$PATH; export PATH 而下面这种会报错呢? $PATH=~/.lib:$PATH; export PATH 当时我的回答是,"shell就是这样子规定的呀"。 回答的同时,也突然间发现有些自己感觉很熟悉的概念,原来自己并没有那么清楚,因此这一篇讲讲shell的命令行