PAT 1126 Eulerian Path[欧拉路][比较]
1126 Eulerian Path (25 分)
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>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std; map<int,int> mp;
int main()
{
int n,m;
cin>>n>>m;
int f,t;
for(int i=;i<m;i++){
cin>>f>>t;
mp[f]++;
mp[t]++;
}
int ct=;//怎么判断map是否到最后一个了呢?
for(auto it=mp.begin();it!=mp.end();){
cout<<it->second;
if(it++!=mp.end())cout<<" ";
if(it->second%!=)
ct++;
}
cout<<"\n";
//如果输入0 0,那么该怎么输出呢?
if(ct==)cout<<"Eulerian";
else if(ct==)cout<<"Semi-Eulerian";
else cout<<"Non-Eulerian"; return ;
}
多种错误
//这个是我写的,但是提交两次,3个格式错误,4个答案错误,一个测试点也没通过。
最终AC:
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
using namespace std; vector<int> vt[];
bool vis[];
map<int,int> mp; void dfs(int f){
vis[f]=true;
for(int i=;i<vt[f].size();i++){
int v=vt[f][i];
if(!vis[v]){
dfs(v);
}
}
}
int main()
{
int n,m;
cin>>n>>m;
int f,t;//需要首先判断图是否连通。
for(int i=;i<m;i++){
cin>>f>>t;
vt[f].push_back(t);
vt[t].push_back(f);
mp[f]++;
mp[t]++;
}
int ct=;//怎么判断map是否到最后一个了呢?
// for(int i=0;i<mp.size();i++){
// cout<<mp[i+1];
// if(i!=mp.size()-1)cout<<" ";
// if(mp[i+1]%2!=0)//如果是奇数
// ct++;
// }
for(int i=;i<=n;i++){
if(i==)cout<<vt[i].size();
else cout<<" "<<vt[i].size();
if(vt[i].size()%)ct++;
} cout<<"\n";
dfs();
int visits=;
for(int i=;i<=n;i++){
if(vis[i])visits++;
}
if(visits!=n){//如果不是连通图。
cout<<"Non-Eulerian";
return ;
}
//如果输入0 0,那么该怎么输出呢?
if(ct==)cout<<"Eulerian";
else if(ct==)cout<<"Semi-Eulerian";
else cout<<"Non-Eulerian"; return ;
}
1.最关键的是需要进行联通判断,前提是必须是连通图
2.还有在进行输出每个点的度的时候,直接输出每个向量的size即可。不知道为什么注释掉的部分使用map就并不正确,有一个测试点4过不去。
#include <iostream>
#include <map>
using namespace std; int main() {
map<int,int> mp;
mp[]=;
mp[]=;
mp[]=;
cout<<mp.size()<<"\n";
for(int i=;i<mp.size();i++)
cout<<mp[i]<<" ";
cout<<"\n";
cout<<mp.size(); return ;
}
输出:
由于i从0开始,一开始map中并没有,所以又进行了一个添加,导致map长度+1,这可能就是出错的原因吧。
PAT 1126 Eulerian Path[欧拉路][比较]的更多相关文章
- PAT 1126 Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT甲级——1126 Eulerian Path
我是先在CSDN上发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89155050 1126 Eulerian Path ( ...
- PAT甲级 1126. Eulerian Path (25)
1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...
- 1126 Eulerian Path (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- PAT 甲级 1126 Eulerian Path
https://pintia.cn/problem-sets/994805342720868352/problems/994805349851185152 In graph theory, an Eu ...
- hdu5883 The Best Path(欧拉路)
题目链接:hdu5883 The Best Path 比赛第一遍做的时候没有考虑回路要枚举起点的情况导致WA了一发orz 节点 i 的贡献为((du[i] / 2) % 2)* a[i] 欧拉回路的起 ...
- hdu_5883_The Best Path(欧拉路)
题目链接:hdu_5883_The Best Path 题意: n 个点 m 条无向边的图,找一个欧拉通路/回路使得这个路径所有结点的异或值最大. 题解: 节点 i 的贡献为((du[i] +1/ 2 ...
- 1126. Eulerian Path (25)
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- PAT A1126 Eulerian Path (25 分)——连通图,入度
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
随机推荐
- IT 服务管理工具 iTop
iTop,作为全面支持ITIL流程的一款ITSM工具,具有强大的ITSM功能,开源免费.简单易用. iTop,即IT运营门户(IT Operation Portal),是一个开源web应用程序,用于I ...
- usb 安装系统
写在前面 本文是先安装windows再安装linux,并通过windows引导linux的启动项.这样方便linux的反复重装.折腾等. 光盘安装和U盘安装基本差不多,只是U盘安装多了把镜像文件写到U ...
- 6:7 题一起MySQL数据库分库备份
企业Shell面试题6:MySQL数据库分表备份 请实现对MySQL数据库进行分表备份,用脚本实现. 解答: [root@db01 scripts]# cat fenbiao.sh #!/bin/ba ...
- idea传入HttpServletRequest时显示cannot resolve symbol的问题
在使用idea ide的时候,在控制器中我需要获取HttpServletRequest这个request对象,编译时出现cannot resolve symbol的问题,然后网上查找资料,这里记录一下 ...
- 参考 generate-parentheses
分析: 关键:当前位置左括号不少于右括号 图是什么? 节点:目前位置左括号和右括号数(x,y)(x>=y) 边:从(x,y)到(x+1,y)和(x,y+1) ...
- easyui -grid每列绑定tooltip
/**用法:*/function doCellTip() { $('#dg').datagrid('doCellTip', { 'max-width': '100px' });} /** * 扩展两个 ...
- [Django] 问题记录追踪表
关注的网站: https://simpleisbetterthancomplex.com/ Linux部署 1. Ubuntu下vsftpd安装部署 2. Utuntu下Django+Apache+W ...
- python 糗事百科实例
爬取糗事百科段子,假设页面的URL是 http://www.qiushibaike.com/8hr/page/1 要求: 使用requests获取页面信息,用XPath / re 做数据提取 获取每个 ...
- ARM汇编(2)(指令)
一,ARM汇编语言立即数的表示方法 十六进制:前缀:0x 十进制:无前缀 二制:前缀:0b 二,常用的ARM指令(标准的ARM语法,GNU的ARM语法) 1.@M开头系列 MOV R0, #12 @R ...
- (转)fock函数详解
转自:http://www.cnblogs.com/bastard/archive/2012/08/31/2664896.html linux中fork()函数详解 一.fork入门知识 一个进程, ...