CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:esya_rae
Submission time:2024-10-23 21:41:53 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.08 sdetails
#20.08 sdetails
#30.08 sdetails
#40.08 sdetails
#50.08 sdetails
#60.98 sdetails
#70.16 sdetails
#80.19 sdetails
#90.20 sdetails
#100.20 sdetails
#110.08 sdetails
#120.24 sdetails

Code

import math
import sys
sys.setrecursionlimit(10**6)
def dfs(v, d):
    global g, node_ids, depths, r
    node_ids.append(v)
    depths.append(d)
    r[v] = len(node_ids)-1
    for w in g[v]:
        dfs(w, d + 1)
        node_ids.append(v)
        depths.append(d)
    visited[v] = True


def build_tree_min(i):
    global depths, min_tree, k, m
    if i >= k + m:
        return math.inf, -1
    if i >= k:
        min_tree[i] = (depths[i - k], i - k)
        return min_tree[i]
    d1, i1 = build_tree_min(2 * i)
    d2, i2 = build_tree_min(2 * i + 1)
    if d1 < d2:
        min_tree[i] = (d1, i1)
    else:
        min_tree[i] = (d2, i2)

    return min_tree[i]


def answer_min(a, b):
    global min_tree, k
    res = math.inf
    value = -1
    a += k
    b += k
    while a <= b:
        if a % 2 == 1:
            z, v = min_tree[a]
            if res > z:
                value = v
                res = z
            a += 1
        if b % 2 == 0:
            z, v = min_tree[b]
            if res > z:
                value = v
                res = z
            b -= 1
        a //= 2
        b //= 2
    return res, value


n, q = map(int, input().split())
node_ids = []
depths = []
g = [[] for _ in range(n)]
r = [-1] * n
x = list(map(int, input().split()))
for i in range(n - 1):
    g[x[i] - 1].append(i + 1)

dfs(0, 0)

m = len(node_ids)
k = 2 ** math.ceil(math.log(m, 2))
min_tree = [math.inf] * (2 * k)
build_tree_min(1)
# print(k, min_tree)

output = []
for i in range(q):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    v, w = answer_min(min(r[a], r[b]), max(r[a], r[b]))
    output.append(str(node_ids[w] + 1))

sys.stdout.write("\n".join(output) + "\n")

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 66, in <module>
    dfs(0,...

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 66, in <module>
    dfs(0,...

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 66, in <module>
    dfs(0,...

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 66, in <module>
    dfs(0,...

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 66, in <module>
    dfs(0,...

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 "input/code.py", line 66, in <module>
    dfs(0,...

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 66, in <module>
    dfs(0,...

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 66, in <module>
    dfs(0,...

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 "input/code.py", line 66, in <module>
    dfs(0,...

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 "input/code.py", line 66, in <module>
    dfs(0,...

Test 11

Verdict:

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

correct output
1
1
1
2

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 66, in <module>
    dfs(0,...

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 "input/code.py", line 66, in <module>
    dfs(0,...