-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patheulerian_path.cpp
More file actions
58 lines (45 loc) · 1.03 KB
/
eulerian_path.cpp
File metadata and controls
58 lines (45 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
// Time complexity O(M*log(M) + N)
int const maxn = 1e5;
set<int>adj[maxn+7];
int degree[maxn+7] = {};
vector<int>path;
void dfs(int a){
while(!adj[a].empty()){
int b = *adj[a].begin();
adj[a].erase(adj[a].begin());
adj[b].erase(a);
dfs(b);
}
path.push_back(a);
}
int main(){
int n, m;cin>>n>>m;
for(int i = 0;i < m;i++){
int a, b;cin>>a>>b;
adj[a].insert(b);
adj[b].insert(a);
degree[a]++;
degree[b]++;
}
// Checking if the given graph has an eulerian path.
int cnt_odd = 0;
int start = 1;
for(int i = 1;i <= n;i++){
if (degree[i]%2 == 1){
cnt_odd++;
start = i;
}
}
if (cnt_odd == 0 || cnt_odd == 2){
dfs(start);
cout << "Eulerian path: ";
for(int a:path) cout << a << " ";
cout << endl;
}
else{
cout << "The given graph does not have an eulerian path." << endl;
}
return 0;
}