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 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:

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[欧拉路][比较]的更多相关文章

  1. PAT 1126 Eulerian Path

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

  2. PAT甲级——1126 Eulerian Path

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

  3. PAT甲级 1126. Eulerian Path (25)

    1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...

  4. 1126 Eulerian Path (25 分)

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

  5. PAT 甲级 1126 Eulerian Path

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

  6. hdu5883 The Best Path(欧拉路)

    题目链接:hdu5883 The Best Path 比赛第一遍做的时候没有考虑回路要枚举起点的情况导致WA了一发orz 节点 i 的贡献为((du[i] / 2) % 2)* a[i] 欧拉回路的起 ...

  7. hdu_5883_The Best Path(欧拉路)

    题目链接:hdu_5883_The Best Path 题意: n 个点 m 条无向边的图,找一个欧拉通路/回路使得这个路径所有结点的异或值最大. 题解: 节点 i 的贡献为((du[i] +1/ 2 ...

  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 A1126 Eulerian Path (25 分)——连通图,入度

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

随机推荐

  1. FastDFS 常见问题

    FastDFS 常见问题 Q:/fdfs_trackerd: error while loading shared libraries: libevent-1.4.so.2: cannot open ...

  2. eclispe luna 安装subversive和svn connector插件

    1. subversive安装 下载地址: http://www.eclipse.org/subversive/latest-releases.php 或者在eclipse luna的marketpl ...

  3. Problem b 莫比乌斯反演+枚举除法的取值

    莫比乌斯反演+枚举除法的取值 第二种形式: f(n)表示gcd(x,y)=n的数量. F(n)表示gcd(x,y)是n的倍数的数量. /** 题目:Problem b 链接:https://vjudg ...

  4. Xcode 调试方法总结

    前言:编写代码过程中出现错误.异常是不可避免的.通常我们都需要进行大量的调试去寻找.解决问题.这时,熟练掌握调试技巧将很大程度上的提高工作效率.接下来就说说开发过程中Xcode的调试方法. 1. En ...

  5. Maven 安装教程

    Linux系统: 1.准本工作 Maven下载地址:http://mirror.bit.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven- ...

  6. The.first.glance.at.linux.commands

    ## Get Ubuntu Version Info lsb_release -a ## Get Linux kernal info uname -a ## Get Computer name ech ...

  7. zend studio 10.6.2 设置默认编码为UTF-8

    如果汉化的:窗体-->常规-->工作空间   然后再选择编码格式 如果未汉化:Window->Preferences->General->wookspace   然后再选 ...

  8. 第二百一十五节,jQuery EasyUI,DateBox(日期输入框)组件

    jQuery EasyUI,DateBox(日期输入框)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 DateBox(日期输入框)组件的使 ...

  9. android Dialog 底部弹出

    . if (dialShareDialog == null) { dialShareDialog = new Dialog(context, R.style.dialog); dialShareDia ...

  10. CornerStone配置SVN,HTTP及SVN简单使用说明

    本文转载至 http://blog.csdn.net/allison162004/article/details/38796857 已经安装了的小伙伴请直接看三步骤 一.下载地址 CornerSton ...