forked from sourav-122/hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler Tour Algorithm.cpp
More file actions
50 lines (40 loc) · 832 Bytes
/
Euler Tour Algorithm.cpp
File metadata and controls
50 lines (40 loc) · 832 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define MAX 1001
vector<int> adj[MAX];
int vis[MAX];
int Euler[2 * MAX];
// Function to add edges to tree
void add_edge(int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void eulerTree(int u, int &indx)
{
vis[u] = 1;
Euler[indx++] = u;
for (auto it : adj[u]) {
if (!vis[it]) {
eulerTree(it, indx);
Euler[indx++] = u;
}
}
}
int main()
{
int N, M;
cin >> N >> M;
for(int i = 0; i < M; i++){
int a,b;
cin >> a >> b;
add_edge(a, b);
}
// Consider 1 as root and 0 as index.
int index = 0;
int root = 1;
eulerTree(root, index);
for (int i = 0; i < (2*N-1); i++) // To print Euler Tour of tree
cout << Euler[i] << " ";
return 0;
}