E - 伝言リレー / Message Relay Editorial by admin Editorial for printing
GLM 5.2 (High)Overview
This problem asks us to quickly find the first student whose academic ability is at least the difficulty of the quiz at the starting point, on a directed graph with an out-degree of 1 (a functional graph).
Analysis
- Since each student passes the quiz to exactly one other student, the transitions form what is known in graph theory as a “functional graph”. A functional graph consists of a set of cycles and trees (tails) rooted on those cycles.
- A naive step-by-step simulation would take \(O(N^2)\) time in the worst case due to traversing long cycles, resulting in a TLE (Time Limit Exceeded).
- To speed up this process, we can use “doubling”. We precompute the destination node after taking \(2^k\) steps from each node, as well as the maximum academic ability along that path.
- However, when searching within a cycle, if the maximum academic ability in the cycle is less than the quiz difficulty, we would fall into an infinite loop. To prevent this, we precompute whether each node belongs to a cycle, the length of the cycle, the maximum academic ability within the cycle, and the distance from each node to the cycle (the length of the tail).
Algorithm
- Creating the Doubling Tables
next_tbl[k][i]: The node reached by taking \(2^k\) steps from node \(i\).max_s_tbl[k][i]: The maximum academic ability along the path of \(2^k\) steps starting from node \(i\).
- Computing Cycle and Tail Information
- Reduce the in-degrees of nodes in a manner similar to topological sorting; nodes with non-zero in-degrees remaining are determined to be part of a cycle.
- For nodes belonging to a cycle, compute the cycle length
loop_lenand the maximum academic ability within the cyclemax_s_loop. - Perform a BFS traversing the edges in reverse starting from the cycle nodes to compute the distance to the cycle
tail_lenfor each node.
- Answering Each Quiz Query
- Starting from student \(i\), we first consider moving up to the “tail length”.
- We iterate through the doubling table in descending order of \(k\). If the “maximum academic ability up to \(2^k\) steps ahead is less than \(D_i\)”, we advance by \(2^k\) steps. Repeating this process allows us to reach the node immediately preceding the first student with an academic ability of \(D_i\) or greater.
- If a student with academic ability \(\ge D_i\) is found within the tail, the next node is the answer.
- If no such student is found even after traversing the entire tail, we enter the cycle. If the maximum academic ability within the cycle is less than \(D_i\), no one can solve it, so the answer is
-1. - If the maximum academic ability within the cycle is at least \(D_i\), we limit the search range to the length of the cycle and similarly use doubling to advance and find the answer.
Complexity
- Time Complexity: \(O(N \log N)\)
- Space Complexity: \(O(N \log N)\)
Implementation Points
When moving to the node just before the one satisfying the condition using doubling, greedily advancing under the condition “the maximum academic ability in the range of \(2^k\) steps is less than \(D_i\)” allows us to process each quiz query in \(O(\log N)\) time.
In the search after reaching the cycle, there is no need to go around the cycle more than once. Thus, we can search correctly by limiting the remaining distance we can advance to
loop_len.Source Code
import sys
def solve():
input = sys.stdin.readline
N = int(input())
P = list(map(lambda x: int(x) - 1, input().split()))
S = list(map(int, input().split()))
D = list(map(int, input().split()))
LOG = 20
next_tbl = [[0] * N for _ in range(LOG)]
max_s_tbl = [[0] * N for _ in range(LOG)]
for i in range(N):
next_tbl[0][i] = P[i]
max_s_tbl[0][i] = S[i]
for k in range(1, LOG):
next_k = next_tbl[k]
next_k_1 = next_tbl[k-1]
max_s_k = max_s_tbl[k]
max_s_k_1 = max_s_tbl[k-1]
for i in range(N):
nxt = next_k_1[i]
next_k[i] = next_k_1[nxt]
m1 = max_s_k_1[i]
m2 = max_s_k_1[nxt]
max_s_k[i] = m1 if m1 >= m2 else m2
indeg = [0] * N
for i in range(N):
indeg[P[i]] += 1
from collections import deque
q = deque()
for i in range(N):
if indeg[i] == 0:
q.append(i)
is_loop = [True] * N
while q:
u = q.popleft()
is_loop[u] = False
v = P[u]
indeg[v] -= 1
if indeg[v] == 0:
q.append(v)
rep = [-1] * N
loop_len = [0] * N
max_s_loop = [0] * N
for i in range(N):
if is_loop[i] and rep[i] == -1:
curr = i
length = 0
max_s = 0
loop_nodes = []
while True:
rep[curr] = i
loop_nodes.append(curr)
length += 1
if S[curr] > max_s:
max_s = S[curr]
curr = P[curr]
if curr == i:
break
for node in loop_nodes:
loop_len[node] = length
max_s_loop[node] = max_s
rev = [[] for _ in range(N)]
for i in range(N):
rev[P[i]].append(i)
tail_len = [0] * N
q = deque()
for i in range(N):
if is_loop[i]:
q.append(i)
while q:
u = q.popleft()
for v in rev[u]:
if not is_loop[v]:
tail_len[v] = tail_len[u] + 1
q.append(v)
ans = [-1] * N
for i in range(N):
d = D[i]
curr = i
rem = tail_len[i]
for k in range(LOG - 1, -1, -1):
if rem >= (1 << k):
if max_s_tbl[k][curr] < d:
curr = next_tbl[k][curr]
rem -= (1 << k)
if rem > 0:
ans[i] = curr + 1
else:
r = rep[curr]
if max_s_loop[r] < d:
ans[i] = -1
else:
rem = loop_len[r]
for k in range(LOG - 1, -1, -1):
if rem >= (1 << k):
if max_s_tbl[k][curr] < d:
curr = next_tbl[k][curr]
rem -= (1 << k)
ans[i] = curr + 1
print(*ans)
if __name__ == '__main__':
solve()
This editorial was generated by or-glm5.2-high.
posted:
last update: