Submission details
Task:Company Queries II
Sender:aalto26gh_003
Submission time:2026-10-18 17:29:51 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.04 sdetails
#20.04 sdetails
#30.04 sdetails
#40.04 sdetails
#50.04 sdetails
#6--details
#70.18 sdetails
#80.21 sdetails
#9--details
#10--details
#110.04 sdetails
#12--details

Code

import sys


def main():
    data = map(int, sys.stdin.buffer.read().split())
    it = iter(data)
    n = next(it)
    q = next(it)
    bosses = [next(it) for _ in range(n - 1)]
    print(n, q, bosses)

    out = []
    app = out.append
    for _ in range(q):
        a = next(it)
        b = next(it)

        while a != b and a != 1 and b != 1:
            a, b = bosses[a - 2], bosses[b - 2]

        if a == 1 or b == 1:
            app("1")
        else:
            app(str(a))

    print(" ".join(out))


main()

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
10 10 [1, 2, 3, 4, 5, 6, 7, 8,...

Feedback: Output is longer than expected

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
10 10 [1, 1, 1, 1, 1, 1, 1, 1,...

Feedback: Output is longer than expected

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
10 10 [1, 1, 1, 1, 2, 3, 4, 4,...

Feedback: Output is longer than expected

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
10 10 [1, 1, 3, 1, 2, 2, 5, 3,...

Feedback: Output is longer than expected

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
10 10 [1, 2, 3, 2, 5, 3, 2, 2,...

Feedback: Output is longer than expected

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
200000 200000 [1, 1, 1, 1, 1, ...

Feedback: Output is longer than expected

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
200000 200000 [1, 2, 1, 2, 3, ...

Feedback: Output is longer than expected

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
2 4 [1]
1 1 1 2

Feedback: Output is longer than expected

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)