| Task: | Company Queries II |
| Sender: | aalto26gh_006 |
| Submission time: | 2026-10-20 13:19:10 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.00 s | details |
| #3 | ACCEPTED | 0.00 s | details |
| #4 | ACCEPTED | 0.00 s | details |
| #5 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 0.67 s | details |
| #7 | ACCEPTED | 0.40 s | details |
| #8 | ACCEPTED | 0.50 s | details |
| #9 | ACCEPTED | 0.68 s | details |
| #10 | ACCEPTED | 0.61 s | details |
| #11 | ACCEPTED | 0.00 s | details |
| #12 | ACCEPTED | 0.74 s | details |
Compiler report
input/code.cpp: In function 'int calc_LCA(int&, int&, std::vector<std::vector<int> >&, std::vector<std::pair<int, int> >&, int)':
input/code.cpp:40:9: warning: unused variable 'n_employees' [-Wunused-variable]
40 | int n_employees = tree.size() - 1;
| ^~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:69:9: warning: unused variable 'max_k' [-Wunused-variable]
69 | int max_k = pow(2, max_k_power);
| ^~~~~Code
#include <iostream>
#include <vector>
#include <cmath>
#include <bits/stdc++.h>
#include <string>
using namespace std;
int succ(int node, int depth, vector<pair<int, int>> & tree)
{
if(depth == 1)
{
return tree[node].first;
}
else
{
return succ(succ(node, depth/2, tree), depth/2, tree);
}
}
void preproc(int max_k_power, vector<vector<int>> & ancestors, vector<pair<int, int>> & tree, vector<int> & depth)
{
int n_employees = tree.size() - 1;
for(int i = 1; i <= max_k_power; ++i)
{
for(int e = 1; e <= n_employees; ++e)
{
ancestors[e][i] = ancestors[ancestors[e][i-1]][i-1];
}
}
}
int calc_LCA(int & node1, int & node2, vector<vector<int>> & ancestors, vector<pair<int, int>> & tree, int max_k_power)
{
if(node1 == node2) return node1;
if(tree[node1].second < tree[node2].second)
{
swap(node1, node2);
}
int n_employees = tree.size() - 1;
int current_diff = tree[node1].second - tree[node2].second;
for(int i = max_k_power; i >= 0; --i)
{
if(pow(2, i) <= current_diff)
{
node1 = ancestors[node1][i];
current_diff = current_diff - pow(2,i);
}
}
if(node1 == node2) return node1;
for(int i = max_k_power; i >= 0; --i)
{
if(ancestors[node1][i] != ancestors[node2][i])
{
node1 = ancestors[node1][i];
node2 = ancestors[node2][i];
}
}
return ancestors[node1][0];
}
int main()
{
long long n_employees;
long long n_queries;
cin >> n_employees >> n_queries;
int max_k_power = floor(log2(n_employees));
int max_k = pow(2, max_k_power);
vector<vector<int>> ancestors(n_employees + 1, vector<int>(max_k_power + 1, 0));
vector<pair<int, int>> tree(n_employees + 1);
vector<int> depth(n_employees + 1, -1);
depth[1] = 0;
tree[1] = {0, 0};
for(int i = 2; i <= n_employees; ++i)
{
int a;
cin >> a;
tree[i] = {a, tree[a].second + 1};
ancestors[i][0] = a;
}
preproc(max_k_power, ancestors, tree, depth);
string bin = "";
for(int i = 0; i < n_queries; ++i)
{
int a, b;
cin >> a >> b;
bin.append(to_string(calc_LCA(a, b, ancestors, tree, max_k_power)) + '\n');
}
cout << bin;
}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 ... |
