-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhld.py
More file actions
57 lines (50 loc) · 1.7 KB
/
hld.py
File metadata and controls
57 lines (50 loc) · 1.7 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
import sys
sys.setrecursionlimit(10**6)
class HLD:
def __init__(self, n, adj, root=1):
self.n = n
self.adj = adj
self.parent = [0] * (n + 1)
self.depth = [0] * (n + 1)
self.heavy = [0] * (n + 1)
self.head = [0] * (n + 1)
self.pos = [0] * (n + 1)
self.cur_pos = 0
self.parent[root] = 0
self.depth[root] = 0
self._dfs(root)
self._decompose(root, root)
def _dfs(self, v):
size = 1
max_c_size = 0
for c in self.adj[v]:
if c != self.parent[v]:
self.parent[c] = v
self.depth[c] = self.depth[v] + 1
c_size = self._dfs(c)
size += c_size
if c_size > max_c_size:
max_c_size = c_size
self.heavy[v] = c
return size
def _decompose(self, v, h):
self.head[v] = h
self.pos[v] = self.cur_pos
self.cur_pos += 1
if self.heavy[v] != 0:
self._decompose(self.heavy[v], h)
for c in self.adj[v]:
if c != self.parent[v] and c != self.heavy[v]:
self._decompose(c, c)
# 경로 쿼리 템플릿
# def query(self, u, v):
# res = 0
# while self.head[u] != self.head[v]:
# if self.depth[self.head[u]] > self.depth[self.head[v]]:
# u, v = v, u
# # res += seg.query(self.pos[self.head[v]], self.pos[v])
# v = self.parent[self.head[v]]
# if self.depth[u] > self.depth[v]:
# u, v = v, u
# # res += seg.query(self.pos[u], self.pos[v])
# return res