-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompanyQueries2.cpp
More file actions
83 lines (73 loc) · 1.71 KB
/
CompanyQueries2.cpp
File metadata and controls
83 lines (73 loc) · 1.71 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//Company Queries II - https://cses.fi/problemset/task/1688
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
void solve() {
int n, q;
cin >> n >> q;
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int p;
cin >> p;
p--;
g[p].push_back(i);
}
vector<int> parent(n), depth(n, 0);
function<void(int, int, int)> dfs = [&](int u, int p, int d) {
parent[u] = p;
depth[u] = d;
for (auto &v: g[u]) {
if (v == p) continue;
dfs(v, u, d + 1);
}
};
dfs(0, 0, 0);
const int LOG = 25;
vector<vector<int>> up(LOG, vector<int>(n));
for (int i = 0; i < n; i++) {
up[0][i] = parent[i];
}
for (int i = 1; i < LOG; i++) {
for (int j = 0; j < n; j++) {
up[i][j] = up[i - 1][up[i - 1][j]];
}
}
auto lca = [&](int u, int v) {
if (depth[u] < depth[v]) {
swap(u, v);
}
for (int k = LOG - 1; k >= 0; k--) {
if (depth[u] - (1 << k) >= depth[v]) {
u = up[k][u];
}
}
if (u == v) {
return u;
}
for (int k = LOG - 1; k >= 0; k--) {
if (up[k][u] != up[k][v]) {
u = up[k][u];
v = up[k][v];
}
}
return parent[u];
};
for (int i = 0; i < q; i++) {
int u, v;
cin >> u >> v;
u--, v--;
cout << lca(u, v) + 1 << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}