Official

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

gpt-6-astra-medium

Overview

We propagate the effects of operational shutdowns to recipients, starting with the broadcasting stations that no longer receive their required materials. By managing the shutting-down stations with a queue, we can determine the shutdown day for each station in \(O(N+M)\) time.

Analysis

The Amount of Materials Received Only Decreases

Since a station that has shut down once never resumes operations, the total amount of materials received by each station each morning will only decrease and never increase.

Therefore, we maintain the current total amount of materials for each station \(i\) as total[i]. Initially, all stations are operational, so:

\(total[i] = \sum_{j:\,v_j=i} W_j\)

If total[i] < B[i] at this point, that station will shut down on the night of Day \(1\).

No Need to Check Everything Every Day

If we recomputed the received amounts by checking all supply relations every day, and the cascading shutdowns continue for about \(N\) days, the time complexity would be \(O(N(N+M))\), which would be too slow.

However, the amount of materials received changes only when a supplying station shuts down.

When station \(u\) shuts down, for each supply relation \(u \to v\) from \(u\), we only need to subtract that amount of materials from total[v]. Since each station shuts down at most once, it is sufficient to process each supply relation only once.

The Impact of a Shutdown Appears on the Following Day

Even if station \(u\) shuts down on the night of Day \(d\), it still supplies materials on the morning of Day \(d\). The supply ceases starting from the morning of Day \(d+1\).

Therefore, if the shutdown of \(u\) causes station \(v\) to experience a shortage for the first time, station \(v\)’s shutdown day is Day \(d+1\). Causing a chain reaction of shutdowns within the same day will lead to a Wrong Answer.

For example, if the required amount for each station is \(1\), and the supply relations are only

\(1 \xrightarrow{1} 2 \xrightarrow{1} 3\)

then station \(1\) will shut down on the night of Day \(1\), station \(2\) on the night of Day \(2\), and station \(3\) on the night of Day \(3\).

Process in Order of Shutdown Day Using a Queue

First, we push all stations that shut down on Day \(1\) into a queue. After that, we pop a station with shutdown day \(d\), and for any newly deficient stations, we set their shutdown day to \(d+1\) and push them to the back of the queue.

Using a FIFO (first-in, first-out) queue allows us to process stations in increasing order of their shutdown days.

Even if multiple suppliers shut down on the same day, we can subtract their supply amounts one by one. Once the received amount falls below the required threshold, subtracting the remaining supplies cannot increase the received amount, so the shutdown on the next day is guaranteed.

Algorithm

We maintain the following information:

  • graph[u]: A list of recipient stations from station \(u\) and the corresponding amounts of materials
  • total[i]: The total amount of materials from suppliers whose shutdowns have not yet been reflected
  • day[i]: The shutdown day of station \(i\). The initial value is \(0\), representing that the shutdown day is undecided
  • Queue: Stations whose shutdown day has been determined, but whose impact on recipients has not yet been processed

The procedure is as follows:

  1. Read all supply relations and construct graph and the initial total.
  2. For each station \(i\), if total[i] < B[i], set day[i] = 1 and push it to the queue.
  3. Repeat the following until the queue is empty:
    • Pop the front station \(u\).
    • Iterate through each supply relation \((u,v,W)\) from \(u\):
      • If day[v] != 0, station \(v\)’s shutdown day is already determined, so do not process it.
      • Otherwise, subtract \(W\) from total[v].
      • If total[v] < B[v], set day[v] = day[u] + 1 and push \(v\) to the queue.
  4. Output day.

Why This Correctly Computes the Shutdown Days

Stations whose initial received amounts are less than their required amounts are precisely those that shut down on Day \(1\).

After that, the queue processes the impacts in order of shutdown day. Once the impacts of all stations shutting down on Day \(d\) are reflected, the total of each station whose shutdown day is undecided becomes the total amount of materials it receives on the morning of Day \(d+1\). Any station that becomes deficient during this process could operate until Day \(d\) and falls short on Day \(d+1\); thus, its shutdown day can be correctly determined as Day \(d+1\).

Stations with day[i] = 0 until the end still meet their requirements even after reflecting the impacts of all shut-down stations. Since they can continue operating using only the supplies among the remaining stations, they will never shut down thereafter.

Complexity

Each station is pushed into the queue at most once, and each supply relation is examined at most once when processing its supplier.

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

Implementation Notes

  • Use 64-bit integers for the total amount of materials
    The total received amount can be up to \(M \times 10^9 \leq 2 \times 10^{14}\), so in C++, long long is required.

  • Use a strict inequality when checking for shortages
    A station can operate as long as it receives at least its required amount, so the shutdown condition is total[i] < B[i].

  • Determine the shutdown day at the time of pushing to the queue
    By excluding stations with day[v] != 0, we avoid pushing the same station into the queue multiple times. Furthermore, there is no need to update the received amounts for stations whose shutdown days have already been determined.

  • Self-loops and multiple edges can be handled as-is
    Materials from self-loops are received on mornings when the station itself is operational. Including them in the initial sum and then ignoring them during the shutdown process (since their shutdown day is already determined) handles them correctly. Multiple edges can simply be added and subtracted independently.

  • The queue can be implemented with a vector and a head pointer
    In the sample code, elements are added with que.push_back() and popped by advancing head. Since the front element is not erased from the vector, each operation runs in constant time.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    vector<long long> B(N), total(N, 0);
    for (auto& b : B) cin >> b;

    struct Edge {
        int to;
        long long weight;
    };
    vector<vector<Edge>> graph(N);

    for (int j = 0; j < M; ++j) {
        int u, v;
        long long w;
        cin >> u >> v >> w;
        --u;
        --v;
        graph[u].push_back({v, w});
        total[v] += w;
    }

    vector<int> day(N, 0);
    vector<int> que;
    que.reserve(N);

    for (int i = 0; i < N; ++i) {
        if (total[i] < B[i]) {
            day[i] = 1;
            que.push_back(i);
        }
    }

    for (size_t head = 0; head < que.size(); ++head) {
        int u = que[head];
        for (const auto& e : graph[u]) {
            int v = e.to;
            if (day[v] != 0) continue;

            total[v] -= e.weight;
            if (total[v] < B[v]) {
                day[v] = day[u] + 1;
                que.push_back(v);
            }
        }
    }

    for (int i = 0; i < N; ++i) {
        if (i) cout << ' ';
        cout << day[i];
    }
    cout << '\n';

    return 0;
}

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

posted:
last update: