| Task: | Company Queries II |
| Sender: | aalto26gh_026 |
| Submission time: | 2026-10-20 19:23:13 +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.44 s | details |
| #7 | ACCEPTED | 0.28 s | details |
| #8 | ACCEPTED | 0.39 s | details |
| #9 | ACCEPTED | 0.45 s | details |
| #10 | ACCEPTED | 0.41 s | details |
| #11 | ACCEPTED | 0.01 s | details |
| #12 | ACCEPTED | 0.49 s | details |
Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 300000;
const int LOG = 18;
vector<int> TREE[MAXN];
int LCA[MAXN][LOG];
int DEPTH[MAXN];
// Find all node depth and fill LCA table
void dfs(int a, int b) {
LCA[a][0] = b;
// Fill LCA
for (int i = 1; i < LOG; i++) {
if (LCA[a][i - 1] != -1) {
LCA[a][i] = LCA[LCA[a][i - 1]][i - 1];
} else {
LCA[a][i] = -1;
}
}
// Go thru children and set depth
for (int u : TREE[a]) {
if (u != b) {
DEPTH[u] = DEPTH[a] + 1;
dfs(u, a);
}
}
}
// Find LCA of a and b
int lca(int a, int b) {
if (DEPTH[a] < DEPTH[b]) {
swap(a, b);
}
// Get depth difference and fix it
int diff = DEPTH[a] - DEPTH[b];
for (int i = 0; i < LOG; i++) {
if ((diff >> i) & 1) {
a = LCA[a][i];
}
}
// If the node was on the same branch, check if it is now b
if (a == b) {
return a;
}
// Both are on the same level, keep moving them up, until they are the same.
for (int i = LOG - 1; i >= 0; i--) {
if (LCA[a][i] != LCA[b][i]) {
a = LCA[a][i];
b = LCA[b][i];
}
}
return LCA[a][0];
}
int main() {
int n, q;
cin >> n >> q; // input(), get from standard input feed
// Build tree
for (int child = 2; child <= n; child++) {
int boss;
cin >> boss;
TREE[boss].push_back(child);
TREE[child].push_back(boss);
}
// Queries
vector<vector<int> > queries(q, vector<int>(2));
for (int query = 0; query < q; query++) {
int a, b;
cin >> a >> b;
queries[query][0] = a;
queries[query][1] = b;
}
DEPTH[1] = 0;
// Run DFS
dfs(1, -1);
for (int i = 0; i < q; ++i) {
int a = queries[i][0];
int b = queries[i][1];
cout << lca(a, b) << "\n";
}
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 ... |
