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. Zabbix exp编写

    #/usr/bin/python #*-*coding=utf-8*-* import urllib logo = '''\n _____ _ _ _ _____ _ |__ /__ _| |__ | ...

  2. TCP/IP笔记(八)应用层协议

    TCP/IP的应用层涵盖了OSI参考模型中第5.第6.第7层的所有功能,不仅包含了管理通信连接的会话层功能.转换数据格式的标识层功能,还包括与对端主机交互的应用层功能在内的所有功能. 利用网络的应用程 ...

  3. 由Python的一个小例子想到的

    习题: L = [1,2] L.append(L) Print L 问,结果是什么. 结果是,[1,2,[...]] 这是什么意思呢?就是说[...]表示的对[1,2]的无限循环.这一点是在C#等静态 ...

  4. liunx下安装eclipse

    1.eclipse-jee-mars-1-linux-gtk-x86_64 下载地址:http://download.csdn.net/download/yichen01010/10018917 2. ...

  5. Easyui控制combotree只能选择叶子节点

    $(function() { $('#tt').combotree({ url: 'getTree.do', onBeforeSelect: function(node) { if (!$(this) ...

  6. Jmeter接口测试系列之参数化方法

    至于参数化的用途,我这里就不多说了,本文主要介绍最全.最强大的参数化方法,对参数化有一个彻底的认识,这里提供了多种参数化方法 1.jmeter参数化之用户变量   在测试计划里面添加一个用户自定义的变 ...

  7. 第一百六十三节,jQuery,基础核心

    jQuery,基础核心 一.代码风格 在jQuery程序中,不管是页面元素的选择.内置的功能函数,都是美元符号“$”来起 始的.而这个“$”就是jQuery当中最重要且独有的对象:jQuery对象,所 ...

  8. 第一百五十八节,封装库--JavaScript,ajax说明

    封装库--JavaScript,ajax说明 封装库ajax()方法,ajax通讯方法,跨页面向动态页面发送或获取数据 /** ajax()方法,ajax通讯方法,跨页面向动态页面发送或获取数据 * ...

  9. 【BZOJ3166】[Heoi2013]Alo 可持久化Trie树+set

    [BZOJ3166][Heoi2013]Alo Description Welcome to ALO ( Arithmetic and Logistic Online).这是一个VR MMORPG , ...

  10. 《JAVA多线程编程核心技术》 笔记:第三章:线程间通信

    一. 等待/通知机制:wait()和notify()1.1.使用的原因:1.2 具体实现:wait()和notify()1.2.1 方法wait():1.2.2 方法notify():1.2.3 wa ...