Submission details
Task:Company Queries II
Sender:aalto26gh_024
Submission time:2026-10-22 11:41:55 +0300
Language:C++ (C++23)
Status:READY
Result:
Test results
testverdicttime
#1--details
#2--details
#3--details
#4--details
#5--details
#6--details
#7--details
#8--details
#9--details
#10--details
#11--details
#12--details

Code

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


struct Element {
    int boss;
    int level;
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    int n, q;
    std::cin >> n >> q;
    std::vector<Element> elements(n + 1);
    elements[1] = Element{1, 1};
    for (int i = 2; i <= n; i++) {
        std::cin >> elements[i].boss;
        elements[i].level = elements[elements[i].boss].level + 1;
    }
    std::vector<std::pair<int, int>> queries(q);
    for (int j = 0; j<q; j++) {
        std::cin >> queries[j].first >> queries[j].second;
    }
    for (int j = 0; j<q; j++) {
        Element a = elements[queries[j].first];
        Element b = elements[queries[j].second];
        while (a.level != b.level) {
            if (a.level < b.level) a = elements[a.boss];
            else b = elements[b.boss];
        }
        while (a.boss != b.boss) {
            if (a.boss > 1) a = elements[a.boss];
            if (b.boss > 1) b = elements[b.boss];
        }
        std::cout << a.boss << std::endl;
    }
}

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
(empty)

Test 2

Verdict:

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
(empty)

Test 3

Verdict:

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
(empty)

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
(empty)

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
(empty)

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
(empty)

Test 7

Verdict:

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
(empty)

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
(empty)

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
(empty)

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
(empty)

Test 11

Verdict:

input
2 4
1
1 1
1 2
2 1
...

correct output
1
1
1
2

user output
(empty)

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
(empty)