-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhld.cpp
More file actions
65 lines (58 loc) · 1.37 KB
/
hld.cpp
File metadata and controls
65 lines (58 loc) · 1.37 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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Heavy-Light Decomposition
const int MAX = 100005;
vector<int> adj[MAX];
int parent[MAX], depth[MAX], heavy[MAX], head[MAX], pos[MAX];
int cur_pos;
// 1. DFS to find heavy edges and subtree sizes
int dfs(int v) {
int size = 1;
int max_c_size = 0;
for (int c : adj[v]) {
if (c != parent[v]) {
parent[c] = v;
depth[c] = depth[v] + 1;
int c_size = dfs(c);
size += c_size;
if (c_size > max_c_size) {
max_c_size = c_size;
heavy[v] = c;
}
}
}
return size;
}
// 2. Decompose chains
void decompose(int v, int h) {
head[v] = h;
pos[v] = cur_pos++;
if (heavy[v] != -1)
decompose(heavy[v], h);
for (int c : adj[v]) {
if (c != parent[v] && c != heavy[v])
decompose(c, c);
}
}
// 초기화
void init_hld(int root) {
fill(heavy, heavy + MAX, -1);
parent[root] = -1;
depth[root] = 0;
cur_pos = 0;
dfs(root);
decompose(root, root);
}
// 경로 쿼리 예시 (Segment Tree 필요)
// int query(int a, int b) {
// int res = 0;
// for (; head[a] != head[b]; b = parent[head[b]]) {
// if (depth[head[a]] > depth[head[b]]) swap(a, b);
// // res += seg.query(pos[head[b]], pos[b]);
// }
// if (depth[a] > depth[b]) swap(a, b);
// // res += seg.query(pos[a], pos[b]);
// return res;
// }