Task: | Company Queries II |
Sender: | minghao |
Submission time: | 2024-10-24 19:29:29 +0300 |
Language: | C++ (C++20) |
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.71 s | details |
#7 | ACCEPTED | 0.53 s | details |
#8 | ACCEPTED | 0.64 s | details |
#9 | ACCEPTED | 0.72 s | details |
#10 | ACCEPTED | 0.67 s | details |
#11 | ACCEPTED | 0.01 s | details |
#12 | ACCEPTED | 0.75 s | details |
Code
#include <cstring> #include <iostream> #include <vector> using namespace std; constexpr int N = 200005, LOG = 20; vector<int> e[N]; int f[N][LOG], d[N]; void DFS(int uno, int fno) { f[uno][0] = fno; d[uno] = d[fno] + 1; for (int i = 1; i < LOG; i++) { f[uno][i] = f[f[uno][i-1]][i-1]; } for(int v: e[uno]) { if (v == fno) continue; DFS(v, uno); } } int LCA(int x, int y) { if (d[x] > d[y]) swap(x, y); int tmp = d[y] - d[x]; for (int i = 0; tmp; i++, tmp >>= 1) if (tmp & 1) { y = f[y][i]; } if(y == x) return y; for (int i = LOG - 1; i >= 0 && y != x; i--) { if (f[x][i] != f[y][i]) { x = f[x][i]; y = f[y][i]; } } return f[y][0]; } int main() { // cin.tie(nullptr)->sync_with_stdio(false); int n, q; cin >> n >> q; for (int i = 2; i <= n; i++) { int x; cin >> x; e[i].push_back(x); e[x].push_back(i); } DFS(1, 0); while(q--) { int a, b; cin >> a >> b; cout << LCA(a, b) << endl; } return 0; }
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 ... |
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 ... |
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 ... |
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 ... |
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 ... |
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 ... |