/tmp/solutions/build/lca-main.cpp:
1| |#include <common.h>
2| |prelude;
3| |
4| |namespace {
5| |
6| |constexpr int N = 5e5;
7| |
8| |int next[N];
9| |int head[N];
10| |int size[N];
11| |int depth[N];
12| |int heavy[N];
13| |int ances[N];
14| |int parent[N];
15| |
16| 5|def build_step_1(int u, int d) -> void {
17| 5| size[u] = 1;
18| 5| depth[u] = d;
19| 9| for (int v = head[u]; v; v = next[v]) {
^4
------------------
| Branch (19:25): [True: 44.44%, False: 55.56%]
------------------
20| 4| build_step_1(v, d + 1);
21| 4| size[u] += size[v];
22| 4| if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
^2
------------------
| Branch (22:9): [True: 50.00%, False: 50.00%]
| Branch (22:26): [True: 0.00%, False: 100.00%]
------------------
23| 2| heavy[u] = v;
24| 2| }
25| 4| }
26| 5|}
27| |
28| 5|def build_step_2(int u, int a) -> void {
29| 5| ances[u] = a;
30| 9| for (int v = head[u]; v; v = next[v]) {
^4
------------------
| Branch (30:25): [True: 44.44%, False: 55.56%]
------------------
31| 4| if (v != heavy[u]) {
------------------
| Branch (31:9): [True: 50.00%, False: 50.00%]
------------------
32| 2| build_step_2(v, v);
33| 2| }
34| 4| }
35| 5| if (heavy[u]) build_step_2(heavy[u], a);
^2
------------------
| Branch (35:7): [True: 40.00%, False: 60.00%]
------------------
36| 5|}
37| |
38| 5|def lca(int u, int v) -> int {
39| 5| if (depth[u] < 32 && depth[v] < 32) {
------------------
| Branch (39:7): [True: 100.00%, False: 0.00%]
| Branch (39:24): [True: 100.00%, False: 0.00%]
------------------
40| 5| while (depth[u] > depth[v]) {
------------------
| Branch (40:12): [True: 0.00%, False: 100.00%]
------------------
41| 0| u = parent[u];
42| 0| }
43| 9| while (depth[v] > depth[u]) {
------------------
| Branch (43:12): [True: 44.44%, False: 55.56%]
------------------
44| 4| v = parent[v];
45| 4| }
46| 7| while (u != v) {
------------------
| Branch (46:12): [True: 28.57%, False: 71.43%]
------------------
47| 2| u = parent[u];
48| 2| v = parent[v];
49| 2| }
50| 5| return u;
51| 5| }
52| 0| while (ances[u] != ances[v]) {
------------------
| Branch (52:10): [True: 0.00%, False: 0.00%]
------------------
53| 0| if (depth[ances[u]] > depth[ances[v]]) {
------------------
| Branch (53:9): [True: 0.00%, False: 0.00%]
------------------
54| 0| u = parent[ances[u]];
55| 0| } else {
56| 0| v = parent[ances[v]];
57| 0| }
58| 0| }
59| 0| return depth[u] < depth[v] ? u : v;
------------------
| Branch (59:10): [True: 0.00%, False: 0.00%]
------------------
60| 5|}
61| |
62| |} // namespace
63| |
64| 1|int main() {
65| 1| rd rd;
66| 1| wt wt;
67| 1| int n = rd.uh();
68| 1| int q = rd.uh();
69| 1|#ifdef LOCAL
70| 1| std::memset(head, 0, 4 * n);
71| 1| std::memset(heavy, 0, 4 * n);
72| 1| std::memset(parent, 0, 4 * n);
73| 1|#endif
74| 5| for (int i = 1; i < n; ++i) {
^4
------------------
| Branch (74:19): [True: 80.00%, False: 20.00%]
------------------
75| 4| int p = rd.uh();
76| 4| parent[i] = p;
77| 4| next[i] = head[p];
78| 4| head[p] = i;
79| 4| }
80| 1| build_step_1(0, 0);
81| 1| build_step_2(0, 0);
82| 6| while (q--) {
------------------
| Branch (82:10): [True: 83.33%, False: 16.67%]
------------------
83| 5| int u = rd.uh();
84| 5| int v = rd.uh();
85| 5| int w = lca(u, v);
86| 5| wt.uw(w);
87| 5| }
88| 1| return 0;
89| 1|}