Submission details
Task:Company Queries II
Sender:aalto26gh_024
Submission time:2026-10-22 15:05:44 +0300
Language:C++ (C++23)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#40.00 sdetails
#50.00 sdetails
#60.69 sdetails
#7ACCEPTED0.15 sdetails
#80.29 sdetails
#90.50 sdetails
#100.41 sdetails
#11ACCEPTED0.00 sdetails
#120.62 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:30:54: warning: '<<' in boolean context, did you mean '<'? [-Wint-in-bool-context]
   30 |         for (int i = 0; i < max; i++) if (diff && (1 << i)) a = bosses[a][i];
      |                                                   ~~~^~~~~

Code

#include <iostream>
#include <vector>
#include <algorithm>



int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    int n, q;
    std::cin >> n >> q;
    std::vector<int> level(n+1);
    int max = 18;
    std::vector<std::vector<int>> bosses(n+1, std::vector<int>(max));
    for (int i = 0; i < max; i++) bosses[1][i] = 1;
    for (int i = 2; i <= n; i++) {
        int tmp;
        std::cin >> tmp;
        level[i] = level[tmp] + 1;
        bosses[i][0] = tmp;
        for (int j = 1; j < max; j++) bosses[i][j] = bosses[bosses[i][j - 1]][j - 1];
    }
    while (q--) {
        int a, b;
        std::cin >> a >> b;
        if (level[a] < level[b]) {
            std::swap(a, b);
        }
        int diff = level[a] - level[b];
        for (int i = 0; i < max; i++) if (diff && (1 << i)) a = bosses[a][i];
        if (a == b) {
            std::cout << a << '\n';
            continue;
        }
        for (int k = max - 1; k >= 0; k--) {
            if (bosses[a][k] != bosses[b][k]) {
                    a = bosses[a][k];
                    b = bosses[b][k];
            }
        }
        std::cout << bosses[a][0] << '\n';
    }
}

Test details

Test 1

Verdict:

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
1
1
1
1
1
...

Feedback: Incorrect character on line 1 col 1: expected "6", got "1"

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:

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
1
2
1
1
1
...

Feedback: Incorrect character on line 1 col 1: expected "2", got "1"

Test 5

Verdict:

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
1
2
...

Feedback: Incorrect character on line 4 col 1: expected "2", got "1"

Test 6

Verdict:

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
1
1
1
1
1
...

Feedback: Incorrect character on line 1 col 1: expected "74862", got "1"

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:

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
1
1
1
1
...

Feedback: Incorrect character on line 2 col 1: expected "2", got "1"

Test 9

Verdict:

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
1
1
1
1
1
...

Feedback: Incorrect character on line 1 col 1: expected "2796", got "1"

Test 10

Verdict:

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
1
1
1
1
1
...

Feedback: Incorrect character on line 1 col 1: expected "365", got "1"

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:

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
1
1
1
1
1
...

Feedback: Incorrect character on line 1 col 1: expected "27468", got "1"