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. SpringBoot+Shiro引起事务失效、错误原因、解决方法

    一.问题今天发现用户注册的Service的事务并没有起到作用,再抛出一个RuntimeException后,并没有发生回滚,下面是调试步骤: 1.检查数据库的引擎是否是innoDB 2.启动类上是否加 ...

  2. Excel函数sumproduct应用案例-多条件求和

    作者:iamlaosong 越来越认为sumproduct这个函数实用,过去用sum组函数.改起来复制起来都麻烦,sumif在条件多的时候也认为不方便. 如今改用sumproduct函数,就简单多了. ...

  3. 提高.net程序性能和稳定性-CLR Profile

    CLR Profile能够看到应用程序的内存堆栈情况并且能够查询垃圾回收机制的行为.利用CLR Profile可以确定你的代码哪儿分配了太多内存,从而导致垃圾回收机制的执行,哪些代码长时间的占有内存. ...

  4. org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in ……

    编程中遇到:org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot ...

  5. vs2003 不断提示 已过期 问题

    工作时曾遇到使用 vs2003 生成后,点击"调试“或者"执行“后不断提示 类似“已过期,是否要重新生成”这样的问题. 当时的情况是 :我要实现的功能和"时间" ...

  6. css背景图片拉伸

    css背景图片拉伸 background-image:url(bg.png); -moz-background-size: 100% 100%; -o-background-size: 100% 10 ...

  7. Linux下面 多线程死锁问题的调试

    最近写服务,经常是单进程,多线程的,加了各种锁,很担心出现死锁问题,专门学习了一下死锁问题的诊断. 死锁 (deallocks): 是指两个或两个以上的进程(线程)在执行过程中,因争夺资源而造成的一种 ...

  8. 图像增强:直方图均衡和小波变换【matlab】

    直方图均衡:统计图像像素灰度的直方图分布.对其进行重新分配以使图像的直方图分布更加均衡. 小波变换:图像轮廓主要体现在低频部分,可以通过对低频分解系数进行增强处理,对高频分解系数进行衰减处理,达到图像 ...

  9. ADO编程:error C2011: 'LockTypeEnum' : 'enum' type redefinition

     C++ Code  123   // Import the ADO type library #import "C:\\Program Files\\Common Files\\syste ...

  10. Configuration注解类 Bean解析顺序

    @PropertySource 加载properties @ComponentScan 扫描包 @Import 依赖的class @ImportResource 依赖的xml @Bean 创建bean ...