| Task: | Company Queries II |
| Sender: | duongha |
| Submission time: | 2025-10-19 17:12:46 +0300 |
| Language: | C++ (C++11) |
| Status: | READY |
| Result: | WRONG ANSWER |
| 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 | WRONG ANSWER | 0.57 s | details |
| #7 | ACCEPTED | 0.38 s | details |
| #8 | ACCEPTED | 0.45 s | details |
| #9 | WRONG ANSWER | 0.57 s | details |
| #10 | WRONG ANSWER | 0.49 s | details |
| #11 | ACCEPTED | 0.01 s | details |
| #12 | WRONG ANSWER | 0.61 s | details |
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int MAXN = 2e5 + 1;
const int U = 10;
int f[MAXN][U];
int n, q;
vector<int>depth(MAXN, 0);
vector<int>a[MAXN];
// void query(int u, int k) {
// for (int i = 0; i <= U; i++) {
// if ((k >> i) & 1) {
// u = f[u][i];
// }
// }
// return u;
// }
void preprocessing() {
for (int i = 1; i < U; i++) {
for (int j = 2; j <= n; j++) {
f[j][i] = f[f[j][i - 1]][i - 1];
// cout << f[f[j][i - 1]][i - 1] << ' ' << f[j][i - 1] << ' ' << i << ' ' << j << endl;
}
}
}
void DFS (int u) {
for (auto v : a[u]) {
depth[v] = depth[u] + 1;
// cout << depth[v] << ' ' << v << endl;
DFS(v);
}
}
int LCA(int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
// cout << depth[a] << ' ' << depth[b] << endl;
int k = depth[a] - depth[b];
for (int i = 0; i <= U; i++) {
if((k >> i) & 1) {
// cout << f[a][i] << ' ' << a << ' ' << i << endl;
a = f[a][i];
}
}
if (a == b) return a;
for (int i = U - 1; i >= 0; i--) {
if (f[a][i] != f[b][i]) {
a = f[a][i];
b = f[b][i];
}
}
return f[a][0];
}
void solve() {
cin >> n >> q;
for (int i = 2; i <= n; i++) {
int tempt;
cin >> tempt;
f[i][0] = tempt;
// cout << f[i][0] << " test" << endl;
a[tempt].push_back(i);
}
depth[1] = 1;
DFS(1);
preprocessing();
for (int i = 1; i <= q; i++) {
int a, b;
cin >> a >> b;
cout << LCA(a, b) << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
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: WRONG ANSWER
| 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 |
|---|
| 165998 45614 173933 89706 101119 ... |
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: WRONG ANSWER
| 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 |
|---|
| 96066 121748 40531 65087 37122 ... |
Test 10
Verdict: WRONG ANSWER
| 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 |
|---|
| 936 722 38333 16690 46982 ... |
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: WRONG ANSWER
| 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 |
|---|
| 180807 123334 116976 70644 135668 ... |
