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
——————————————————————————————
题目的意思是给出n条边的关系,输出每个点的度和判断是欧拉回路还是欧拉路径还是什么都不是
思路:开数组保存每个点的度,先并查集找出是否是一个连通图,再找出度为奇数的点的数量没如果没有则为欧拉回路,如果是2个则为欧拉路径,否则什么都不是
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int inf=0x3f3f3f3f; int cnt[10005];
int pre[10005];
void init()
{
for(int i=0; i<10005; i++)
pre[i]=i;
} int fin(int x)
{
return x==pre[x]?x:pre[x]=fin(pre[x]);
} int main()
{
int m,n,u,v;
scanf("%d%d",&m,&n);
memset(cnt,0,sizeof cnt);
init();
for(int i=0; i<n; i++)
{
scanf("%d%d",&u,&v);
int a=fin(u);
int b=fin(v);
if(a!=b)
pre[a]=b;
cnt[u]++,cnt[v]++;
}
int ans=0;
int q=0;
int flag=0;
for(int i=1; i<=m; i++)
{
if(cnt[i]%2) ans++;
if(pre[i]==i) flag++;
if(q++)
printf(" ");
printf("%d",cnt[i]);
}
printf("\n");
if(flag==1)
printf("%s\n",ans==0?"Eulerian":(ans<=2)?"Semi-Eulerian":"Non-Eulerian");
else
printf("Non-Eulerian\n");
return 0;
}

PAT甲级 1126. Eulerian Path (25)的更多相关文章

  1. PAT甲级——1126 Eulerian Path

    我是先在CSDN上发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89155050 1126 Eulerian Path ( ...

  2. PAT 甲级 1126 Eulerian Path

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

  3. 1126. Eulerian Path (25)

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

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

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

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

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

  6. 1126 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[欧拉路][比较]

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

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

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

  9. PAT 1126 Eulerian Path

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

随机推荐

  1. Linux netstat

    一.简介   二.语法   三.实例 1)查看TCP连接数 netstat -n | awk '/^tcp/ {++S[$NF]} END {for (a in S) print a, S[a]}'

  2. PAT 1002 写出这个数 (20)(代码)

    1002 写出这个数 (20)(20 分) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10^100 ...

  3. CH6202 黑暗城堡

    一道最短路+生成树 原题链接 实际上就是生成树的中每个点到节点\(1\)的距离等于原图中这个点到节点\(1\)的最短距离,求这样的生成树的棵数. 先用\(SPFA\)或\(Dijkstra\)求出所有 ...

  4. mysql5.6改进子查询实测试

    表t1,t2 各自生成100万条记录. 表引擎  myiasm  ,查询语句 select * from t1 where id2 in (select id2 from t2 ) 查询速度 2.x秒 ...

  5. MySQL 系列(一)安装

    MySQL 系列(一)安装 以 Centos7 下安装 MySQL 5.6 为例. 一.环境准备 (1) 下载 下载地址: https://dev.mysql.com/get/Downloads/My ...

  6. MySQL复制(Replication)

    引自:http://www.cnblogs.com/hustcat/archive/2009/12/19/1627525.html 1.复制概述 1.1.复制解决的问题数据复制技术有以下一些特点:(1 ...

  7. jquery查找frameset框架内iframe的元素

    老系统还幸存有过时的frameset框架,维护升级工作需要对其内部的iframe中的元素进行相关操作.使用jquery查找子iframe页面内的元素时,总找不到目标元素.后来发现少了contents ...

  8. 关于部署传统的Dynamic Web项目

    现在大部分都是采用maven构建的项目,但是偶尔也会遇到一些较老的项目,采用的是传统的动态Web项目. 我最近碰到这样一个项目,项目用的jar包都放在了WEB-INF/lib目录下.之前的人采用的部署 ...

  9. Python之tornado

    一.快速了解Tornado框架 1)tornado简介 Tornado 和现在的主流 Web 服务器框架(包括大多数 Python 的框架)有着明显的区别:它是非阻塞式服务器,而且速度相当快.得利于其 ...

  10. 2018.12.05 codeforces 948C. Producing Snow(堆)

    传送门 维护一个堆. 每次先算出一个都不弹掉的总贡献. 然后把要弹掉的弹掉,并减去它们对应的贡献. 代码: #include<bits/stdc++.h> #define ri regis ...