-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patheulertour.cpp
More file actions
74 lines (70 loc) · 1.66 KB
/
eulertour.cpp
File metadata and controls
74 lines (70 loc) · 1.66 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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long int
#define pb push_back
#define pi pair<int, int>
#define pii pair<pi, int>
#define fir first
#define sec second
#define DEBUG 1
#define MAXN 100001
#define mod 1000000009
#define d 31
int n, idx;
vector<int> adj[MAXN];
int euler[2 * MAXN];
int entrei[MAXN];
int sai[MAXN];
void euler_tour(int s, int f)
{
euler[idx] = s;
entrei[s] = idx;
idx++;
for (auto const &v : adj[s])
{
if (v == f)
continue;
euler_tour(v, s);
}
euler[idx] = s;
sai[s] = idx;
idx++;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
euler_tour(0, -1);
for (int i = 0; i < 2 * n; i++)
cout << euler[i] << " ";
cout << endl;
return 0;
}
// euler tour of a tree
// muito util para algumas coisas
// exemplos:
// 1- soma da subarvore de v(com update)
// usando segment trees, podemos fazer uma query(entrei[v], sai[v])
// 2- LCA
// lca(u, v) = query(entrei[u], entrei[v])
// usando uma query de minimo e considerando as profundidade dos vertices
// a resposta sera o vertice de profundidade minima que encontrarmos no intervalo
// 3- agilidade para remover arestas/vertices/subtrees da arvore
// basta apenas tratar o segmento equivalente do jeito que for necessario
// 4- reroot a tree
// basta apenas rotacionar o euler path