D - 素材が減る放送局 / Broadcasting Station with Dwindling Materials Editorial by admin Editorial for printing
claude5.5opus-highOverview
When a station halts, the amount of material received by stations that were receiving from it decreases. This can trigger a chain reaction where other stations also halt. We can determine on which day each station halts by tracking the chain of halts layer by layer, similar to BFS.
Analysis
The amount received decreases monotonically The amount of material that station \(v\) receives on the morning of day \(d\) is the sum of the weights \(W\) of the edges \(u \to v\) from “stations \(u\) that are operating on the morning of day \(d\)”. Since the number of operating stations only decreases over time, the received amount can only decrease (or stay the same) each day.
Stations that halt are connected to stations that halted the previous day - Stations that halt on day 1 are those whose total incoming edge weight \(S_v\) is strictly less than \(B_v\). - Consider a station \(v\) that halts for the first time on day \(d \ge 2\). If its received amount did not decrease from the morning of the previous day, it would not halt today either, because it was already at least \(B_v\) on the previous day. - Therefore, \(v\) must have an incoming edge from a station that halted on the night of day \(d-1\). The only reason for the received amount to decrease is stations that newly halted on that day.
Naive simulation is too slow Recalculating the received amount for all stations every day takes \(O(N+M)\) per day. For example, in a straight chain like \(1 \to 2 \to 3 \to \cdots \to N\), one station halts per day, taking around \(N\) days. The total time complexity becomes \(O(N(N+M))\), resulting in TLE.
Solution: Update only the changes We maintain the “current received amount” \(S_v\) for each station. When station \(u\) halts, we simply subtract \(W\) from \(S_v\) for each outgoing edge from \(u\). This way, we only need to inspect stations whose received amounts have decreased (i.e., stations that might halt).
For example, suppose station \(1\) has \(B_1=5\) with no incoming edges, and station \(2\) has \(B_2=3\) with only an edge \(1\to2\) (weight \(4\)). - Day 1: Since \(S_1=0<5\), station \(1\) halts. - Day 2: Since \(S_2 = 4-4 = 0 < 3\), station \(2\) halts.
The answer is 1 2.
Algorithm
- For all edges, compute \(S_v \mathrel{+}= W\). This represents the received amount on the morning of day 1.
- For stations with \(S_i < B_i\), set
day[i] = 1and add them to the setfrontier. - For \(d = 2, 3, \ldots\), repeat the following until
frontierbecomes empty:- For each station \(u\) in
frontier, iterate over its outgoing edges \(u \to v\) (weight \(W\)). - If \(v\) is still operating, update \(S_v \mathrel{-}= W\).
- If this results in \(S_v < B_v\), set
day[v] = dand add \(v\) to the nextfrontier.
- For each station \(u\) in
- Stations where
dayremains \(0\) until the end will continue operating indefinitely.
Handling simultaneous halts - In the process for day \(d\), we only subtract the contributions from stations that halted up to day \(d-1\). Therefore, once all subtractions are complete, \(S_v\) corresponds to the “received amount on the morning of day \(d\)”. - It is safe to trigger the halt as soon as \(S_v < B_v\) becomes true. Since the value only decreases further, it will definitely remain strictly less than \(B_v\) at the end of the day. - Outgoing edges of stations decided to halt on day \(d\) will be processed in the next layer. Therefore, the rule where stations “halt simultaneously on the same night” is correctly simulated.
Complexity
- Time Complexity: \(O(N+M)\)
- Each station enters
frontierat most once, and each edge is processed at most once.
- Each station enters
- Space Complexity: \(O(N+M)\)
Implementation Details
Representing the adjacency list in CSR format: Compute the starting index of outgoing edges for each station using prefix sums of out-degrees, and store them in flat arrays. In Python, this is faster and uses less memory than creating a list of lists.
Self-loops and edges to already halted stations: Check if a station is still operating using
day[v] == 0, and ignore edges to already halted stations. A self-loop \(u \to u\) is also naturally skipped since \(u\) has already halted.Multiple edges: Parallel edges can simply be subtracted individually as separate edges, requiring no special handling.
Overflow: \(S_v\) can be up to around \(2\times10^5 \times 10^9 = 2\times10^{14}\). Please use 64-bit integers in languages like C++.
Fast I/O: Since the input is large, read the entire input at once using
sys.stdin.buffer.read().Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
n = int(data[0]); m = int(data[1])
B = [int(x) for x in data[2:2+n]]
S = [0]*n
U = [0]*m; V = [0]*m; Wt = [0]*m
deg = [0]*(n+1)
p = 2+n
for j in range(m):
u = int(data[p])-1; v = int(data[p+1])-1; w = int(data[p+2]); p += 3
U[j] = u; V[j] = v; Wt[j] = w
S[v] += w
deg[u+1] += 1
for i in range(n):
deg[i+1] += deg[i]
start = deg[:]
pos = deg[:n]
adjV = [0]*m; adjW = [0]*m
for j in range(m):
u = U[j]
k = pos[u]
adjV[k] = V[j]; adjW[k] = Wt[j]
pos[u] = k+1
day = [0]*n
frontier = []
for i in range(n):
if S[i] < B[i]:
day[i] = 1
frontier.append(i)
d = 1
while frontier:
d += 1
nxt = []
for u in frontier:
for k in range(start[u], start[u+1]):
v = adjV[k]
if day[v] == 0:
s = S[v] - adjW[k]
S[v] = s
if s < B[v]:
day[v] = d
nxt.append(v)
frontier = nxt
sys.stdout.write(" ".join(map(str, day)) + "\n")
main()
This editorial was generated by claude5.5opus-high.
posted:
last update: