Submission details
Task:Company Queries II
Sender:aalto26gh_026
Submission time:2026-10-20 18:57:41 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.04 sdetails
#3ACCEPTED0.04 sdetails
#4ACCEPTED0.04 sdetails
#5ACCEPTED0.04 sdetails
#60.52 sdetails
#7ACCEPTED0.74 sdetails
#8--details
#90.52 sdetails
#100.54 sdetails
#11ACCEPTED0.04 sdetails
#120.52 sdetails

Code

## Find lowest common ancestor in tree.
#https://www.geeksforgeeks.org/dsa/cses-solutions-company-queries-ii/

# Lowest common ancestor
LCA = []
# Depth of nodes
DEPTH = []
# Parent to child
TREE = []

LOG = 18

## Find depth of all nodes and initialize LCA
def dfs(a, b):
    # First LCA is b
    LCA[a][0] = b

    # Fill LCA. Ancestor DP table.
    for i in range(1, LOG):
        if LCA[a][i - 1] != -1:
            LCA[a][i] = LCA[LCA[a][i - 1]][i - 1]
        else:
            LCA[a][i] = -1

    # Explore children. Set same depth + 1.
    for u in TREE[a]:
        if u != b:
            DEPTH[u] = DEPTH[a] + 1
            dfs(u, a)

# Find LCA of a and b
def lca(a, b):
    if DEPTH[a] < DEPTH[b]:
        a, b = b, a

    # What is the difference in depth between a and b.
    diff = DEPTH[a] - DEPTH[b]
    for i in range(LOG):
        if (diff >> i) & 1:
            a = LCA[a][i]

    # Check if we have reached the parent
    if a == b:
        return a

    # Same level, but in a differnet branch
    for i in range(LOG - 1, -1, -1):
        if LCA[a][i] != LCA[b][i]:
            a = LCA[a][i]
            b = LCA[b][i]

    return LCA[a][0]


def main():
    global TREE, LCA, DEPTH
    
    read = input().split()
    n = int(read[0])
    q = int(read[1])

    e = (int(x) for x in input().split())
    queries = [[0, 0]] * q
    for query in range(q):
        queries[query] = [int(x) for x in input().split()]

    # Build tree
    TREE = [[] for _ in range(n + 1)]
    for child, boss in enumerate(e, start=2):
        TREE[boss].append(child)
        TREE[child].append(boss)

    # Lowest common ancestor table size
    LCA = [[-1] * LOG for _ in range(n + 1)]
    # Depth for each node
    DEPTH = [0] * (n + 1)

    dfs(1, -1)

    output = []
    for query in queries:
        a = query[0]
        b = query[1]
        output.append(str(lca(a, b)))

    for out in output:
        print(out)

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)

Error:
Traceback (most recent call last):
  File "/box/input/code.py", line 90, in <module>
    main()
  File "/box/input/code.py", line 79, in main
    dfs(1, -1)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  [Previous line repeated 1330 more times]
  File "/box/input/code.py", line 20, in dfs
    for i in range(1, LOG):
RecursionError: maximum recursion depth exceeded

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

Error:
Traceback (most recent call last):
  File "/box/input/code.py", line 90, in <module>
    main()
  File "/box/input/code.py", line 79, in main
    dfs(1, -1)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  [Previous line repeated 3379 more times]
  File "/box/input/code.py", line 20, in dfs
    for i in range(1, LOG):
RecursionError: maximum recursion depth exceeded

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)

Error:
Traceback (most recent call last):
  File "/box/input/code.py", line 90, in <module>
    main()
  File "/box/input/code.py", line 79, in main
    dfs(1, -1)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  [Previous line repeated 1994 more times]
  File "/box/input/code.py", line 20, in dfs
    for i in range(1, LOG):
RecursionError: maximum recursion depth exceeded

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)

Error:
Traceback (most recent call last):
  File "/box/input/code.py", line 90, in <module>
    main()
  File "/box/input/code.py", line 79, in main
    dfs(1, -1)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  File "/box/input/code.py", line 30, in dfs
    dfs(u, a)
  [Previous line repeated 1330 more times]
  File "/box/input/code.py", line 20, in dfs
    for i in range(1, LOG):
RecursionError: maximum recursion depth exceeded