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,这可能就是出错的原因吧。