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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! HL 分解。

use std::fmt::Debug;

/// HL 分解。
///
/// <https://codeforces.com/blog/entry/53170>, Easiest HLD with subtree queries.
///
/// <https://atcoder.jp/contests/abc294/submissions/39923465>
#[derive(Clone, Debug)]
pub struct Hld {
    perm: Vec<usize>,
    perm_inv: Vec<usize>,
    inout: Vec<(usize, usize)>,
    heavy: Vec<usize>,
    par: Vec<usize>,
    depth: Vec<usize>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HlEdge {
    Heavy(usize, usize),
    Light(usize, usize),
}
use HlEdge::{Heavy, Light};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Direction {
    Asc,
    Desc,
}
use Direction::{Asc, Desc};

impl HlEdge {
    pub fn rev(self) -> Self {
        match self {
            Heavy(u, v) => Heavy(v, u),
            Light(u, v) => Light(v, u),
        }
    }
    pub fn inner(self) -> (usize, usize) {
        match self {
            Heavy(u, v) | Light(u, v) => (u, v),
        }
    }
}

impl Hld {
    // g[v] は、子方向への隣接頂点のみを持つとする。
    pub fn new(mut g: Vec<Vec<usize>>, r: usize) -> Self {
        let n = g.len();
        let mut size = vec![0; n];
        Self::dfs_size(&mut g, r, &mut size);

        let perm = Self::dfs_order(&g, r);
        let perm_inv = Self::inv(&perm);
        let g = Self::relabel(&g, &perm);
        let (inout, heavy) = Self::dfs_hld(&g, r);

        let (par, depth) = Self::par_depth(&g, r);
        Self { perm, perm_inv, inout, heavy, par, depth }
    }

    pub fn lca_decoded(&self, ou: usize, ov: usize) -> usize {
        let u = self.perm[ou];
        let v = self.perm[ov];
        let w = self.lca(u, v);
        self.perm_inv[w]
    }

    pub fn path(
        &self,
        ou: usize,
        ov: usize,
    ) -> impl Iterator<Item = (HlEdge, Direction)> {
        let u = self.perm[ou];
        let v = self.perm[ov];
        let w = self.lca(u, v);
        let asc = self.ascend(u, w).map(|e| (e, Asc));
        let desc = self.ascend(v, w).map(|e| (e.rev(), Desc)).rev();
        asc.chain(desc)
    }

    pub fn subtree_range(&self, ov: usize) -> (usize, usize) {
        self.inout[self.perm[ov]]
    }

    pub fn encode(&self, ov: usize) -> usize { self.perm[ov] }
    pub fn decode(&self, v: usize) -> usize { self.perm_inv[v] }

    fn lca(&self, u: usize, v: usize) -> usize {
        let dh = |v| self.depth[self.heavy[v]];
        let (mut lo, mut hi) = if dh(u) > dh(v) { (u, v) } else { (v, u) };

        while self.heavy[lo] != self.heavy[hi] {
            lo = self.par[self.heavy[lo]];
            if dh(lo) < dh(hi) {
                std::mem::swap(&mut lo, &mut hi);
            }
        }
        if self.depth[lo] > self.depth[hi] { hi } else { lo }
    }

    fn dfs_size(g: &mut [Vec<usize>], v: usize, size: &mut [usize]) {
        size[v] = 1;
        if g[v].is_empty() {
            return;
        }
        for i in 0..g[v].len() {
            let nv = g[v][i];
            Self::dfs_size(g, nv, size);
            size[v] += size[nv];
            if size[nv] > size[g[v][0]] {
                g[v].swap(0, i);
            }
        }
    }

    fn dfs_order(g: &[Vec<usize>], r: usize) -> Vec<usize> {
        fn dfs(g: &[Vec<usize>], v: usize, t: &mut usize, order: &mut [usize]) {
            order[v] = *t;
            *t += 1;
            for &nv in &g[v] {
                dfs(g, nv, t, order);
            }
        }

        let n = g.len();
        let mut t = 0;
        let mut order = vec![0; n];
        dfs(g, r, &mut t, &mut order);
        order
    }

    fn inv(p: &[usize]) -> Vec<usize> {
        let n = p.len();
        let mut q = vec![0; n];
        for i in 0..n {
            q[p[i]] = i;
        }
        q
    }

    fn relabel(g: &[Vec<usize>], p: &[usize]) -> Vec<Vec<usize>> {
        let n = g.len();
        let mut h = vec![vec![]; n];
        for v in 0..n {
            for &nv in &g[v] {
                h[p[v]].push(p[nv]);
            }
        }
        h
    }

    fn dfs_hld(
        g: &[Vec<usize>],
        r: usize,
    ) -> (Vec<(usize, usize)>, Vec<usize>) {
        fn dfs(
            g: &[Vec<usize>],
            v: usize,
            inout: &mut [(usize, usize)],
            next: &mut [usize],
            t: &mut usize,
        ) {
            inout[v].0 = *t;
            *t += 1;
            for &nv in &g[v] {
                next[nv] = if nv == g[v][0] { next[v] } else { nv };
                dfs(g, nv, inout, next, t);
            }
            inout[v].1 = *t;
            *t += 1;
        }

        let n = g.len();
        let mut inout = vec![(0, 0); n];
        let mut next = vec![0; n];
        let mut t = 0;
        dfs(g, r, &mut inout, &mut next, &mut t);
        (inout, next)
    }

    fn par_depth(g: &[Vec<usize>], r: usize) -> (Vec<usize>, Vec<usize>) {
        fn dfs(
            g: &[Vec<usize>],
            v: usize,
            par: &mut [usize],
            depth: &mut [usize],
        ) {
            for &nv in &g[v] {
                par[nv] = v;
                depth[nv] = depth[v] + 1;
                dfs(g, nv, par, depth);
            }
        }

        let n = g.len();
        let mut par = vec![0; n];
        let mut depth = vec![0; n];
        dfs(g, r, &mut par, &mut depth);
        (par, depth)
    }

    fn ascend(
        &self,
        mut v: usize,
        r: usize,
    ) -> impl Iterator<Item = HlEdge> + DoubleEndedIterator {
        let mut res = vec![];
        while self.heavy[v] != self.heavy[r] {
            if self.heavy[v] == v {
                res.push(Light(v, self.par[v]));
                v = self.par[v];
            } else {
                res.push(Heavy(v, self.heavy[v]));
                v = self.heavy[v];
            }
        }
        if v != r {
            res.push(Heavy(v, r));
        }
        res.into_iter()
    }
}