-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path61 Tree Flattening.cpp
62 lines (47 loc) · 975 Bytes
/
61 Tree Flattening.cpp
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
/**
* Tree Flattening
* ---------------
* The subtree of v can be found in the range st[v] ... en[v]
*/
#include<bits/stdc++.h>
using namespace std;
struct Graph
{
int n;
bool dir;
vector<vector<int>>adj;
// tree flatten
vector<int>st,en;
int timer = 0;
//Queries
Graph(int n,bool dir)
{
this->n = n;
this->dir = dir;
int len = n+1;
adj = vector<vector<int>>(len);
st = vector<int>(len);
en = vector<int>(len);
}
void add_edge(int u,int v)
{
adj[u].push_back(v);
if(!dir) adj[v].push_back(u);
}
void tree_flatten(int u,int p)
{
st[u] = ++timer;
for(int v:adj[u])
{
if(v==p) continue;
tree_flatten(v,u);
}
en[u] = timer;
}
void pre()
{
tree_flatten(1,-1);
// update the data structure data from the flattened tree
// build
}
};