-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleHeavyLightDecomposition.cpp
More file actions
65 lines (63 loc) · 1.75 KB
/
SimpleHeavyLightDecomposition.cpp
File metadata and controls
65 lines (63 loc) · 1.75 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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
// subtree(v): [in[v], out[v])
// heavy path(v, the last vertex in ascending heavy path from v): [in[next[v]], in[v]]
struct SimpleHeavyLightDecomposition {
int n, t = 0;
vector<int> in, out, next, sz, rin;
vector<vector<int>> g;
SimpleHeavyLightDecomposition(int _n, int root, vector<vector<int>> const &_g) {
n = _n;
g = _g;
in.resize(n);
out.resize(n);
next.resize(n);
sz.resize(n);
rin.resize(n + 1);
dfs(root);
hld(root);
}
void dfs(int u) {
sz[u] = 1;
for (auto &v : g[u]) {
dfs(v);
sz[u] += sz[v];
if (sz[v] > sz[g[u][0]]) {
swap(v, g[u][0]);
}
}
}
void hld(int u) {
in[u] = t ++;
rin[in[u]] = u;
for (auto v : g[u]) {
next[v] = (v == g[u][0] ? next[u] : v);
hld(v);
}
out[u] = t;
}
};
int main() {
int n;
scanf("%d", &n);
vector<vector<int>> g(n);
for (int i = 1; i < n; i ++) {
int p;
scanf("%d", &p);
p --;
g[p].push_back(i);
}
SimpleHeavyLightDecomposition hld(n, 0, g);
for (int i = 0; i < n; i ++) {
}
return 0;
}