-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbest euler tour
More file actions
80 lines (64 loc) · 1.1 KB
/
best euler tour
File metadata and controls
80 lines (64 loc) · 1.1 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector<int> gr[N];
int tin[N], tout[N], tme = 0;
int flat[N];
void dfs1(int cur, int par) {
tin[cur] = tme++;
for (auto x : gr[cur]) {
if (x != par) {
// x is child node
dfs1(x, cur);
}
}
tout[cur] = tme++;
}
void dfs2(int cur, int par) {
cout << cur << " ";
for (auto x : gr[cur]) {
if (x != par) {
// x is child node
dfs2(x, cur);
cout << cur << " ";
}
}
}
void dfs3(int cur, int par) {
tin[cur] = ++tme;
for (auto x : gr[cur]) {
if (x != par) {
// x is child node
dfs3(x, cur);
}
}
tout[cur] = tme;
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
gr[x].push_back(y);
gr[y].push_back(x);
}
// tme = 1;
// dfs1(1, 0);
// dfs2(1, 0);
tme = 0;
dfs3(1, 0);
for (int i = 1; i <= n; i++) {
cout << i << " " << tin[i] << " " << tout[i] << '\n';
}
for (int i = 1; i <= n; i++) {
flat[tin[i]] = i;
}
for (int i = 1; i <= n; i++) {
cout << flat[i] << " ";
}
return 0;
}