Submission details
Task:Company Queries II
Sender:aalto26gh_024
Submission time:2026-10-22 14:15:56 +0300
Language:C++ (C++23)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.00 sdetails
#3ACCEPTED0.00 sdetails
#40.00 sdetails
#50.00 sdetails
#60.43 sdetails
#7ACCEPTED0.42 sdetails
#80.57 sdetails
#90.44 sdetails
#100.41 sdetails
#110.00 sdetails
#120.43 sdetails

Code

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


struct Element {
    int boss;
    int level;
    std::vector<int> bosses = {};
};

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;
        elements[i].bosses = elements[elements[i].boss].bosses;
        elements[i].bosses.push_back(elements[i].boss);
    }
    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];
        if (a.level < b.level) {
            int diff = b.level - a.level;
            for (int k = 0; (1 << k) <= diff; k++) {
                if (diff & (1 << k)) {
                    b = elements[b.bosses[k]];
                }
            }
        } else if (a.level > b.level) {
            int diff = a.level - b.level;
            for (int k = 0; (1 << k) <= diff; k++) {
                if (diff & (1 << k)) {
                    a = elements[a.bosses[k]];
                }
            }
        }
        if (a.boss == b.boss) {
            std::cout << a.boss << std::endl;
        } else {
            int max_log = a.bosses.size();
            for (int k = max_log - 1; k >= 0; k--) {
                if (a.bosses[k] != b.bosses[k]) {
                    a = elements[a.bosses[k]];
                    b = elements[b.bosses[k]];
                }
            }
            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
5
7
2

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

Feedback: Incorrect character on line 7 col 1: expected "4", got "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
2
2
...

Feedback: Incorrect character on line 6 col 1: expected "3", 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
(empty)

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

Feedback: Incorrect character on line 7 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
(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
1
1
1
1

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

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)