CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:Farah
Submission time:2024-10-13 20:59:02 +0300
Language:Python3 (CPython3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.02 sdetails
#3ACCEPTED0.02 sdetails
#4ACCEPTED0.02 sdetails
#5ACCEPTED0.02 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details
#11ACCEPTED0.02 sdetails
#12--details

Code

import sys
import sys
import sys
import sys
from math import log2, ceil
import sys
def main():
    import sys
    import sys
    from collections import deque

    sys.setrecursionlimit(1 << 25)
    input = sys.stdin.read().split()
    ptr = 0

    n = int(input[ptr]); ptr += 1
    q = int(input[ptr]); ptr += 1

    # Initialize adjacency list
    adj = [[] for _ in range(n + 1)]

    # Read bosses for employees 2 to n
    for child in range(2, n + 1):
        boss = int(input[ptr]); ptr +=1
        adj[boss].append(child)

    LOG = ceil(log2(n)) if n >1 else 1
    up = [[-1]*(n +1) for _ in range(LOG)]
    depth = [0]*(n +1)

    # BFS to compute depth and up[0][v]
    queue = deque()
    queue.append(1)
    up[0][1] = -1  # root has no parent

    while queue:
        node = queue.popleft()
        for child in adj[node]:
            depth[child] = depth[node] +1
            up[0][child] = node
            queue.append(child)

    # Precompute up tables
    for k in range(1, LOG):
        for v in range(1, n +1):
            if up[k-1][v] != -1:
                up[k][v] = up[k-1][up[k-1][v]]
            else:
                up[k][v] = -1

    # Function to find LCA
    def lca(a, b):
        if depth[a] < depth[b]:
            a, b = b, a
        # Bring a to the same depth as b
        for k in reversed(range(LOG)):
            if up[k][a] != -1 and depth[up[k][a]] >= depth[b]:
                a = up[k][a]
        if a == b:
            return a
        # Now lift both a and b
        for k in reversed(range(LOG)):
            if up[k][a] != -1 and up[k][a] != up[k][b]:
                a = up[k][a]
                b = up[k][b]
        return up[0][a]

    output = []
    for _ in range(q):
        a = int(input[ptr]); ptr +=1
        b = int(input[ptr]); ptr +=1
        output.append(str(lca(a,b)))

    print('\n'.join(output))

if __name__ == "__main__":
    main()

Test details

Test 1

Verdict: ACCEPTED

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
6
8
3
1
8
...

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: ACCEPTED

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

Test 5

Verdict: ACCEPTED

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

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