Submission details
Task:Company Queries II
Sender:aalto26gh_004
Submission time:2026-10-22 13:47:40 +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
#60.40 sdetails
#70.40 sdetails
#80.44 sdetails
#90.57 sdetails
#100.51 sdetails
#110.04 sdetails
#120.56 sdetails

Code

n,q = map(int, input().split())
p = [0, 1] + list(map(int,input().split()))
 
d = [0]*(n+1)
for i in range(2, n + 1):
    d[i] = d[p[i]] + 1
 
L = max(1, n.bit_length())
up = [p]
 
for k in range(1, L):
    t = up[-1]
    row = [0] * (n+1)
 
    for i in range(1, n+1):
        row[i] = t[t[i]]
 
    up.append(row)
 
 
for i in range(q):
    a, b = map(int, input().split())
 
    if d[a] < d[b]:
        a,b = b,a
 
    diff = d[a]-d[b]
    for k in range(L):
 
        if diff >> k & 1:
            
            a = up[k][a]
 
    if a != b:
        for k in range(L-1, -1,-1):
 
            if up[k][a] != up[k][b]:
 
                a,b = up[k][a],up[k][b]
 
        a = p[a]
 

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)

Feedback: Output is shorter 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
(empty)

Feedback: Output is shorter 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
(empty)

Feedback: Output is shorter 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
(empty)

Feedback: Output is shorter 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
(empty)

Feedback: Output is shorter 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)

Feedback: Output is shorter than expected

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)

Feedback: Output is shorter 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
(empty)

Feedback: Output is shorter 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)

Feedback: Output is shorter than expected

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)

Feedback: Output is shorter than expected

Test 11

Verdict:

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

correct output
1
1
1
2

user output
(empty)

Feedback: Output is shorter 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)

Feedback: Output is shorter than expected