| Task: | Company Queries II |
| Sender: | kookinnam |
| Submission time: | 2025-10-19 01:23:18 +0300 |
| Language: | Python3 (PyPy3) |
| Status: | READY |
| Result: | WRONG ANSWER |
| test | verdict | time | |
|---|---|---|---|
| #1 | WRONG ANSWER | 0.04 s | details |
| #2 | WRONG ANSWER | 0.04 s | details |
| #3 | WRONG ANSWER | 0.04 s | details |
| #4 | WRONG ANSWER | 0.04 s | details |
| #5 | WRONG ANSWER | 0.04 s | details |
| #6 | RUNTIME ERROR | 0.51 s | details |
| #7 | WRONG ANSWER | 0.82 s | details |
| #8 | TIME LIMIT EXCEEDED | -- | details |
| #9 | RUNTIME ERROR | 0.53 s | details |
| #10 | TIME LIMIT EXCEEDED | -- | details |
| #11 | WRONG ANSWER | 0.04 s | details |
| #12 | RUNTIME ERROR | 0.53 s | details |
Code
def dfs(v, p):
up[0][v] = p
for u in adj[v]:
if u != p:
depth[u] = depth[v] + 1
dfs(u, v)
def lca(a, b):
if depth[a] < depth[b]:
a, b = b, a
# Lift a up to depth[b]
diff = depth[a] - depth[b]
for i in range(LOG):
if diff & (1 << i):
a = up[i][a]
if a == b:
return a
# Lift a and b up until their parents are equal
for i in reversed(range(LOG)):
if up[i][a] != up[i][b]:
a = up[i][a]
b = up[i][b]
return up[0][a]
if __name__ == "__main__":
n, q = map(int, input().split())
bosses = list(map(int, input().split()))
queries = []
for _ in range(q):
a, b = map(int, input().split())
queries.append([a, b])
# Build adjacency list for the tree
adj = [[] for _ in range(n + 1)]
for i, b in enumerate(bosses, start=2):
adj[b].append(i)
LOG = 20
up = [[0] * (n + 1) for _ in range(LOG)]
depth = [0] * (n + 1)
dfs(1, 0)
print(adj)
print(depth)
# Precompute all 2^j ancestors
for j in range(1, LOG):
for v in range(1, n + 1):
up[j][v] = up[j - 1][up[j - 1][v]]
for i in range(q):
print(lca(queries[i][0], queries[i][1]))
Test details
Test 1
Verdict: WRONG ANSWER
| 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 |
|---|
| [[], [2], [3], [4], [5], [6], ... |
Test 2
Verdict: WRONG ANSWER
| 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 |
|---|
| [[], [2, 3, 4, 5, 6, 7, 8, 9, ... |
Test 3
Verdict: WRONG ANSWER
| 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 |
|---|
| [[], [2, 3, 4, 5, 10], [6], [7... |
Test 4
Verdict: WRONG ANSWER
| 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, 3, 5], [6, 7], [4, 9]... |
Test 5
Verdict: WRONG ANSWER
| 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 |
|---|
| [[], [2], [3, 5, 8, 9], [4, 7]... |
Test 6
Verdict: RUNTIME ERROR
| 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 51, in <module>
dfs(1, 0)
File "input/code.py", line 6, in dfs
dfs(u, v)
File "input/code.py", line 6, in dfs
dfs(u, v)
File "input/code.py", line 6, in dfs
dfs(u, v)
[Previous line repeated 1576 more times]
File "input/code.py", line 2, in dfs
up[0][v] = p
RecursionError: maximum recursion depth exceededTest 7
Verdict: WRONG ANSWER
| 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 |
|---|
| [[], [2, 3, 4, 5, 6, 7, 8, 9, ... |
Test 8
Verdict: TIME LIMIT EXCEEDED
| 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: RUNTIME ERROR
| 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 51, in <module>
dfs(1, 0)
File "input/code.py", line 6, in dfs
dfs(u, v)
File "input/code.py", line 6, in dfs
dfs(u, v)
File "input/code.py", line 6, in dfs
dfs(u, v)
[Previous line repeated 1582 more times]
File "input/code.py", line 2, in dfs
up[0][v] = p
RecursionError: maximum recursion depth exceededTest 10
Verdict: TIME LIMIT EXCEEDED
| 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: WRONG ANSWER
| input |
|---|
| 2 4 1 1 1 1 2 2 1 ... |
| correct output |
|---|
| 1 1 1 2 |
| user output |
|---|
| [[], [2], []] [0, 0, 1] 1 1 1 ... |
Test 12
Verdict: RUNTIME ERROR
| 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 51, in <module>
dfs(1, 0)
File "input/code.py", line 6, in dfs
dfs(u, v)
File "input/code.py", line 6, in dfs
dfs(u, v)
File "input/code.py", line 6, in dfs
dfs(u, v)
[Previous line repeated 1576 more times]
File "input/code.py", line 2, in dfs
up[0][v] = p
RecursionError: maximum recursion depth exceeded