Task: | Company Queries II |
Sender: | Niilo |
Submission time: | 2024-10-10 17:14:18 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.01 s | details |
#2 | ACCEPTED | 0.01 s | details |
#3 | ACCEPTED | 0.01 s | details |
#4 | ACCEPTED | 0.01 s | details |
#5 | ACCEPTED | 0.01 s | details |
#6 | ACCEPTED | 0.72 s | details |
#7 | ACCEPTED | 0.58 s | details |
#8 | ACCEPTED | 0.67 s | details |
#9 | ACCEPTED | 0.73 s | details |
#10 | ACCEPTED | 0.72 s | details |
#11 | ACCEPTED | 0.01 s | details |
#12 | ACCEPTED | 0.74 s | details |
Code
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 18; vector<int> V[1<<N]; int D[1<<N]; int T[N][1<<N]; int fp(int i, int s) { for (int j = 0; j < N; ++j) { if (s&(1<<j)) { i = T[j][i]; } } return i; } int fca(int a, int b) { if (D[a] < D[b]) swap(a,b); a = fp(a, D[a] - D[b]); if (a == b) return a; for (int i = N-1; i >= 0; --i) { if (T[i][a] != T[i][b]) { a = T[i][a]; b = T[i][b]; } } return T[0][a]; } void dfs(int i, int p, int d) { D[i] = d; T[0][i] = p; for (int j : V[i]) { if (j != p) dfs(j,i,d+1); } } int main() { int n, q; cin >> n >> q; for (int i = 1; i < n; ++i) { int a; cin >> a; V[a-1].push_back(i); V[i].push_back(a-1); } dfs(0,0,0); for (int i = 1; i < N; ++i) { for (int j = 0; j < n; ++j) { T[i][j] = T[i-1][T[i-1][j]]; } } for (int i = 0; i < q; ++i) { int a, b; cin >> a >> b; cout << fca(a-1, b-1) + 1 << '\n'; } }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
10 10 1 2 3 4 5 6 7 8 9 6 9 8 10 10 3 ... |
correct output |
---|
6 8 3 1 8 ... |
user output |
---|
6 8 3 1 8 ... |
Test 2
Verdict: ACCEPTED
input |
---|
10 10 1 1 1 1 1 1 1 1 1 1 7 3 4 4 1 ... |
correct output |
---|
1 1 1 1 1 ... |
user output |
---|
1 1 1 1 1 ... |
Test 3
Verdict: ACCEPTED
input |
---|
10 10 1 1 1 1 2 3 4 4 1 1 8 2 7 8 3 ... |
correct output |
---|
1 1 1 1 1 ... |
user output |
---|
1 1 1 1 1 ... |
Test 4
Verdict: ACCEPTED
input |
---|
10 10 1 1 3 1 2 2 5 3 9 7 2 7 6 3 9 ... |
correct output |
---|
2 2 3 1 1 ... |
user output |
---|
2 2 3 1 1 ... |
Test 5
Verdict: ACCEPTED
input |
---|
10 10 1 2 3 2 5 3 2 2 4 6 1 1 3 1 9 ... |
correct output |
---|
1 1 1 2 2 ... |
user output |
---|
1 1 1 2 2 ... |
Test 6
Verdict: ACCEPTED
input |
---|
200000 200000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
74862 8750 16237 72298 58111 ... |
user output |
---|
74862 8750 16237 72298 58111 ... Truncated |
Test 7
Verdict: ACCEPTED
input |
---|
200000 200000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
correct output |
---|
1 1 1 1 1 ... |
user output |
---|
1 1 1 1 1 ... Truncated |
Test 8
Verdict: ACCEPTED
input |
---|
200000 200000 1 2 1 2 3 2 1 6 3 1 10 12 13 4... |
correct output |
---|
1 2 2 2 1 ... |
user output |
---|
1 2 2 2 1 ... Truncated |
Test 9
Verdict: ACCEPTED
input |
---|
200000 200000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
2796 633 633 151 2690 ... |
user output |
---|
2796 633 633 151 2690 ... Truncated |
Test 10
Verdict: ACCEPTED
input |
---|
200000 200000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
365 73 103 365 216 ... |
user output |
---|
365 73 103 365 216 ... Truncated |
Test 11
Verdict: ACCEPTED
input |
---|
2 4 1 1 1 1 2 2 1 ... |
correct output |
---|
1 1 1 2 |
user output |
---|
1 1 1 2 |
Test 12
Verdict: ACCEPTED
input |
---|
200000 200000 1 1 2 3 4 5 6 7 8 9 10 11 12 1... |
correct output |
---|
27468 6353 27468 6353 6353 ... |
user output |
---|
27468 6353 27468 6353 6353 ... Truncated |