Task: | Company Queries II |
Sender: | Farah |
Submission time: | 2024-10-13 20:57:35 +0300 |
Language: | Python3 (CPython3) |
Status: | READY |
Result: | TIME LIMIT EXCEEDED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.02 s | details |
#2 | ACCEPTED | 0.02 s | details |
#3 | ACCEPTED | 0.02 s | details |
#4 | ACCEPTED | 0.02 s | details |
#5 | ACCEPTED | 0.02 s | details |
#6 | TIME LIMIT EXCEEDED | -- | details |
#7 | TIME LIMIT EXCEEDED | -- | details |
#8 | TIME LIMIT EXCEEDED | -- | details |
#9 | TIME LIMIT EXCEEDED | -- | details |
#10 | TIME LIMIT EXCEEDED | -- | details |
#11 | ACCEPTED | 0.02 s | details |
#12 | TIME LIMIT EXCEEDED | -- | details |
Code
import sys sys.setrecursionlimit(300000) LOG = 18 # This is because 2^18 > 2 * 10^5 def dfs(v, p, d): parent[v][0] = p # 2^0-th parent is the direct parent depth[v] = d for u in tree[v]: if u != p: dfs(u, v, d + 1) def preprocess_lca(n): # Compute all 2^i ancestors for each node for i in range(1, LOG): for v in range(1, n + 1): if parent[v][i - 1] != -1: parent[v][i] = parent[parent[v][i - 1]][i - 1] def lca(a, b): # Ensure that a is deeper than b if depth[a] < depth[b]: a, b = b, a # Lift a up until it's at the same depth as b for i in range(LOG - 1, -1, -1): if depth[a] - (1 << i) >= depth[b]: a = parent[a][i] if a == b: return a # Lift both a and b up until their parents are the same for i in range(LOG - 1, -1, -1): if parent[a][i] != parent[b][i]: a = parent[a][i] b = parent[b][i] return parent[a][0] # Reading input n, q = map(int, input().split()) bosses = list(map(int, input().split())) tree = [[] for _ in range(n + 1)] parent = [[-1] * LOG for _ in range(n + 1)] depth = [-1] * (n + 1) # Construct the tree for i in range(2, n + 1): boss = bosses[i - 2] tree[boss].append(i) tree[i].append(boss) # Perform DFS to set up the parent and depth arrays dfs(1, -1, 0) preprocess_lca(n) # Process the queries for _ in range(q): a, b = map(int, input().split()) print(lca(a, b))
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: TIME LIMIT EXCEEDED
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: TIME LIMIT EXCEEDED
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: 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: TIME LIMIT EXCEEDED
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: 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: 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: TIME LIMIT EXCEEDED
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) |