| 78 | } |
| 79 | |
| 80 | struct lca_tree { |
| 81 | int *label; |
| 82 | int *orig; |
| 83 | int *start; |
| 84 | vector<ll> s; |
| 85 | segment_tree st; |
| 86 | |
| 87 | lca_tree(int n, int root, vi *children) { |
| 88 | label = new int[n]; |
| 89 | orig = new int[n]; |
| 90 | start = new int[n]; |
| 91 | memset(label, -1, n << 2); |
| 92 | int at = 0; |
| 93 | queue<int> Q; |
| 94 | Q.push(root); |
| 95 | label[root] = at++; |
| 96 | while (!Q.empty()) { |
| 97 | int cur = Q.front(); |
| 98 | orig[label[cur]] = cur; |
| 99 | Q.pop(); |
| 100 | for (int i = 0; i < size(children[cur]); i++) { |
| 101 | int nxt = children[cur][i]; |
| 102 | if (label[nxt] == -1) { |
| 103 | label[nxt] = at++; |
| 104 | Q.push(nxt); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | seq(children, root); |
| 110 | st = segment_tree(s); |
| 111 | } |
| 112 | |
| 113 | void seq(vi *c, int r) { |
| 114 | start[label[r]] = size(s); |
| 115 | s.push_back(label[r]); |
| 116 | for (int i = 0; i < size(c[r]); i++) { |
| 117 | seq(c, c[r][i]); |
| 118 | s.push_back(label[r]); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | int lca(int a, int b) { |
| 123 | a = label[a]; |
| 124 | b = label[b]; |
| 125 | int x = start[a], |
| 126 | y = start[b]; |
| 127 | if (x > y) swap(x, y); |
| 128 | return orig[st.query(x, y).x]; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | void test() { |
| 133 | int tests = 100; |
nothing calls this directly
no outgoing calls
no test coverage detected