-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path62 Tree Flattening Euler Tour.cpp
68 lines (53 loc) · 1.28 KB
/
62 Tree Flattening Euler Tour.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
63
64
65
66
67
68
/**
* Tree Flattening Euler Version
* -----------------------------
* Each node has 2 position in the newly built array : 1 in the starting time and 1 in the ending time
*
* This type of flattening is important when doing path queries in trees .
* We can only do path queries for those operations only which can be done and undone like sum , XOR etc
*
* In the starting time index we do the operation
* and in the ending time index we undo the operation
*/
#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 euler(int u,int p)
{
st[u] = ++timer;
for(int v:adj[u])
{
if(v==p) continue;
euler(v,u);
}
en[u] = ++timer;
}
void pre()
{
euler(1,-1);
// update the data structure data from the flattened tree
// build
}
};