D - 素材が減る放送局 / Broadcasting Station with Dwindling Materials Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
In this problem, we simulate a process where, among \(N\) broadcasting stations, those whose total amount of received materials falls below a threshold sequentially shut down operations. We need to determine on which day each station shuts down (or if it never shuts down).
Analysis
Naive Approach and Its Issues
One possible approach is to naively simulate the process each day by calculating the total amount of materials sent from currently operating stations for every station, and shutting down any station that falls below its threshold \(B_i\).
However, in cases where shutdowns proceed by only “one station per day,” the simulation can take up to \(N\) days. Re-examining all supply relationships (\(M\) edges) every day results in a total time complexity of \(O(NM)\). Given the constraints \(N, M \leq 200000\), this requires up to roughly \(4 \times 10^{10}\) operations, which will exceed the time limit (TLE).
Efficient Solution
A station that has shut down will never resume operations. Therefore, instead of “recalculating everything from scratch every day,” it is efficient to take a difference-updating (event-driven) approach: “when a station shuts down, subtract the materials it was providing from the stations that received materials from it.”
This is very similar to Kahn’s algorithm used for topological sorting on directed graphs (the method of pushing vertices into a queue once their in-degree becomes 0) and Breadth-First Search (BFS).
- Initially, assume all stations are operating and calculate the total amount of materials received by each station.
- Stations with strictly less than \(B_i\) materials on the morning of Day 1 are guaranteed to shut down on the night of Day 1.
- For each station \(v\) that was receiving materials from a shut-down station \(u\), subtract the amount of materials \(W\) provided by \(u\).
- If this subtraction causes the received materials of station \(v\) to fall below \(B_v\) for the first time, station \(v\) is determined to shut down on the “night of the next day.”
With this method, each supply relationship (edge) is inspected at most once, specifically when the source station shuts down, allowing for fast processing.
Algorithm
Initialization:
- Calculate the total amount of materials received by each station \(v\),
in_weight[v]. - Store each supply relationship \(u \to v\) (amount \(w\)) in an adjacency list.
- Initialize an array
ansto store the answers with \(0\).
- Calculate the total amount of materials received by each station \(v\),
Identify stations shutting down on Day 1:
- For all stations \(i\), if
in_weight[i] < B[i], it will shut down on Day 1, so setans[i] = 1and push it into the queuecurrent_q.
- For all stations \(i\), if
Day-by-day BFS Simulation:
- Start with
day = 1and repeat the following while the queue is not empty:- Pop each station \(u\) that shut down on that day (
day). - For each station \(v\) that station \(u\) supplied materials to:
- If station $v$ is still operating (`ans[v] == 0`), subtract $w$ from `in_weight[v]`. - If the remaining materials after subtraction fall below $B_v$, station $v$ will shut down on the next day, so set `ans[v] = day + 1` and add it to the next day's queue `next_q`. - Once all stations in the current day have been processed, replace
current_qwithnext_qand incrementdayby 1.
- Pop each station \(u\) that shut down on that day (
- Start with
Output the Result:
- Output each element of the array
ans(stations that never shut down remain \(0\)).
- Output each element of the array
Complexity
- Time Complexity: \(O(N + M)\)
- Each station enters the queue at most once.
- Each supply relationship (edge) is referenced at most once, when the source station shuts down.
- Therefore, the overall process runs in time proportional to the number of vertices and edges, easily finishing within the time limit.
- Space Complexity: \(O(N + M)\)
- It uses \(O(N + M)\) memory for the graph’s adjacency list, the amount of materials received by each station, the answer array, etc.
Implementation Notes
Multiple edges and self-loops:
- Even if \(u = v\) (self-loop) or multiple supplies to the same pair (multiple edges) exist, the material is subtracted independently for each edge, so the exact same logic works correctly without special handling (in the case of a self-loop, when the station itself shuts down,
ans[v] != 0already holds, so it is ignored).
- Even if \(u = v\) (self-loop) or multiple supplies to the same pair (multiple edges) exist, the material is subtracted independently for each edge, so the exact same logic works correctly without special handling (in the case of a self-loop, when the station itself shuts down,
Fast I/O:
In Python, when the input size is large (\(N, M \le 200000\)), reading all inputs at once using
sys.stdin.read().split()helps prevent I/O bottlenecks.Source Code
import sys
def main():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
M = int(input_data[1])
B = [0] * (N + 1)
for i in range(1, N + 1):
B[i] = int(input_data[1 + i])
adj = [[] for _ in range(N + 1)]
in_weight = [0] * (N + 1)
idx = 2 + N
for _ in range(M):
u = int(input_data[idx])
v = int(input_data[idx + 1])
w = int(input_data[idx + 2])
idx += 3
adj[u].append((v, w))
in_weight[v] += w
ans = [0] * (N + 1)
current_q = []
for i in range(1, N + 1):
if in_weight[i] < B[i]:
ans[i] = 1
current_q.append(i)
day = 1
while current_q:
next_q = []
for u in current_q:
for v, w in adj[u]:
if ans[v] == 0:
in_weight[v] -= w
if in_weight[v] < B[v]:
ans[v] = day + 1
next_q.append(v)
current_q = next_q
day += 1
sys.stdout.write(" ".join(map(str, ans[1:])) + "\n")
if __name__ == "__main__":
main()
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: