Official

D - 素材が減る放送局 / Broadcasting Station with Dwindling Materials Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

If a broadcasting station halts its operations due to a shortage of materials, other stations receiving materials from it may also halt on subsequent days. By processing halting stations sequentially using a queue and subtracting only the materials that will no longer be provided, we can determine the halt date for each station.

Analysis

Received Materials Can Only Decrease

Since a station never resumes operations once it halts, the total amount of materials each station receives every morning can only decrease over time, never increase.

Initially, all stations are operating, so the total amount of materials station \(i\) receives on the morning of Day 1 is:

\(S_i = \sum_{j:\,v_j=i} W_j\)

If \(S_i < B_i\), that station will halt on the night of Day 1.

Subsequently, when station \(u\) halts, it is sufficient to look only at the supply relationships originating from \(u\). For each supply relationship \((u,v,W)\), the materials receivable by station \(v\) decrease by \(W\).

Checking Everything Every Day Is Too Slow

One might consider checking all supply relationships every day and recalculating the materials received by each station.

However, if the halts cascade one station at a time, halts could occur across up to \(N\) days. Spending \(O(N+M)\) each day would result in an overall time complexity of \(O(N(N+M))\), which will not pass within the time limit.

Therefore, we should only update the recipients affected by the stations that have halted.

The Effect of a Halt Manifests the Next Day

Even if station \(u\) halts on the night of Day \(d\), it still provided materials on the morning of Day \(d\). Those materials disappear on the morning of Day \(d+1\).

Therefore, if station \(u\)’s halt causes station \(v\) to run short of materials, the halt day for station \(v\) is Day \(d+1\). A cascading halt must not occur within the same day.

For example, suppose each station requires 1 material, and there are only the following two supply relationships:

  • 1 material from station 1 to station 2
  • 1 material from station 2 to station 3

Station 1 receives no materials, so it halts on Day 1. Station 2 halts on Day 2, and station 3 halts on Day 3, so the answer is 1 2 3.

Such day-by-day cascading can be handled with a process similar to Breadth-First Search (BFS) using a FIFO queue.

Algorithm

Prepare the following data:

  • outgoing[u]: A list of supply relationships originating from station \(u\)
  • received[i]: The total materials on the first day minus the supplies from already processed halted stations
  • answer[i]: The halt day of station \(i\) (\(0\) if not yet determined)
  • queue: A queue for processing stations whose halt days have been determined

1. Calculate the total materials for the first day

For each supply relationship \((u,v,W)\), do the following:

  • Add \((v,W)\) to outgoing[u]
  • Add \(W\) to received[v]

2. Register stations that halt on Day 1

For all stations \(i\), if received[i] < required[i]:

  • Set answer[i] = 1
  • Push station \(i\) into the queue

3. Propagate the effects of halts

Pop station \(u\) from the front of the queue. Let \(d=\text{answer}[u]\) be the halt day of station \(u\).

For each supply relationship \((u,v,W)\) originating from station \(u\), if station \(v\)’s halt day is not yet determined, do the following:

  1. Subtract \(W\) from received[v].
  2. If received[v] < required[v], set answer[v] = d+1 and push station \(v\) into the queue.

Repeat until the queue becomes empty. For stations whose halt day is never determined, the answer is \(0\).

Why this yields the correct halt days

The stations initially pushed into the queue all have a halt day of 1. Furthermore, any station newly added while processing a station with halt day \(d\) will always have a halt day of \(d+1\).

By using a FIFO queue, stations are processed in non-decreasing order of their halt days. Therefore, while processing stations with halt day \(d\), we are sequentially removing the supplies that are lost on the night of Day \(d\).

Even if multiple stations halt on the same day, the total amount of materials can only decrease. If it falls below the required amount midway, it will definitely remain insufficient after processing all stations halting on that day. Thus, it is correct to determine its halt on the next day immediately.

Moreover, when the queue becomes empty, any station with an answer of \(0\) still meets its required amount even after removing all supplies from halted stations. Since the remaining stations can continue operating on their own, they will never halt in the future.

Complexity

Each station enters the queue at most once. In addition, each supply relationship is inspected at most once, specifically when its source station is popped from the queue.

  • Time complexity: \(O(N+M)\)
  • Space complexity: \(O(N+M)\)

Implementation Notes

  • Use strict inequality when checking for shortages
    A station can continue operating if it receives exactly the required amount, so the condition is received[i] < required[i].

  • Determine the halt day at the moment of pushing into the queue
    Setting answer[v] beforehand prevents the same station from being pushed into the queue multiple times. Furthermore, there is no need to update the received materials for stations whose halt days have already been determined.

  • When using a Python list as a queue, manage the front position
    pop(0) takes \(O(K)\) time due to shifting elements. In the provided code, by incrementing a head index to pop elements, each operation takes \(O(1)\).

  • Large integers are required for material totals
    The total materials can be up to \(M \times 10^9 \leq 2 \times 10^{14}\). While Python handles arbitrarily large integers natively, 64-bit integer types must be used in other languages.

  • Self-supplies and multiple supply relationships can be handled as-is
    Self-supplies are also included in the Day 1 material totals. If there are multiple supply relationships between the same pair of stations, adding and subtracting each of them independently works correctly.

    Source Code

import sys


def main():
    input = sys.stdin.buffer.readline
    n, m = map(int, input().split())
    required = list(map(int, input().split()))

    outgoing = [[] for _ in range(n)]
    received = [0] * n

    for _ in range(m):
        u, v, w = map(int, input().split())
        u -= 1
        v -= 1
        outgoing[u].append((v, w))
        received[v] += w

    answer = [0] * n
    queue = []

    for i in range(n):
        if received[i] < required[i]:
            answer[i] = 1
            queue.append(i)

    head = 0
    while head < len(queue):
        u = queue[head]
        head += 1
        next_day = answer[u] + 1

        for v, w in outgoing[u]:
            if answer[v] == 0:
                received[v] -= w
                if received[v] < required[v]:
                    answer[v] = next_day
                    queue.append(v)

    sys.stdout.write(" ".join(map(str, answer)) + "\n")


if __name__ == "__main__":
    main()

This editorial was generated by gpt-6-astra-medium.

posted:
last update: