D - 素材が減る放送局 / Broadcasting Station with Dwindling Materials Editorial by admin Editorial for printing
importedOverview
When broadcasting stations shut down simultaneously each day due to a shortage of materials, we find the shutdown day for each station. We only remove provisions from stations that have shut down, propagating the effects in chronological order.
Analysis
A naive approach of re-checking all provision relationships every morning would take \(N\) days in the case where stations shut down one by one, resulting in a time complexity of \(O(N(N+M))\).
Since the set of operating stations only decreases, the total amount of materials received by each station also only decreases. Therefore, we can precalculate the initial total amount of materials for each station, and whenever station \(u\) shuts down, subtract the corresponding provision amounts from the totals of the destination stations for all relationships originating from \(u\). Each station shuts down at most once, and each provision relationship is removed at most once.
To find the shutdown days correctly, we must respect the simultaneous shutdowns. The impact of station \(u\) shutting down on the night of day \(d\) manifests starting on the morning of day \(d+1\). Thus, the shutdown day of a station that experiences a shortage for the first time due to this impact is \(d+1\).
We set the shutdown day to \(1\) for stations that already have a shortage from the beginning and push them all into a FIFO queue. When we pop a station with shutdown day \(d\), any newly added station has a shutdown day of \(d+1\). Therefore, stations are popped from the queue in non-decreasing order of their shutdown days. This prevents stations from different days from being mixed up and causing premature shutdowns.
Correctness
The initial total is precisely the amount received on day \(1\) when all stations are operating. Since we set the shutdown day to \(1\) only for stations whose total is strictly less than their required amount, the initial shutdown days are found correctly.
Next, suppose the shutdown days up to day \(d\) have been determined correctly, and we are currently popping stations with shutdown day \(d\) from the queue. All provisions from stations with shutdown day \(\le d-1\) have already been removed, and provisions from stations with shutdown day \(\ge d+1\) have not been removed yet.
As we sequentially remove the provisions from each station with shutdown day \(d\), the running total monotonically decreases from the amount received on day \(d\) to the amount received on day \(d+1\). For a station whose shutdown day is not yet determined, if its total falls below the required amount during this process, it did not have a shortage on day \(d\), but it does have a shortage on day \(d+1\). Thus, setting its shutdown day to \(d+1\) is correct. Changing the order in which relationships are removed on the same day does not affect whether a station ultimately experiences a shortage.
Stations that still do not experience a shortage after processing all stations with shutdown day \(d\) can remain in operation on day \(d+1\). Repeating this process by day ensures that all determined shutdown days are correct and that no shutting-down station is missed. When the queue becomes empty, the materials reaching stations with undetermined shutdown days are precisely the provisions from the remaining stations. Since every remaining station receives at least its required amount, none of them will shut down subsequently, and their answer is \(0\).
Algorithm
- Construct an adjacency list grouped by source station, and compute the initial total amount of materials received by each station.
- Initialize all answers to \(0\). For stations whose initial total is strictly less than their required amount, set their answer to \(1\) and push them into the queue.
- Pop a station \(u\) from the queue. For each provision relationship \((u,v,w)\) originating from \(u\), subtract \(w\) from the total of \(v\).
- If the answer for \(v\) is still \(0\) and its total becomes less than the required amount, set the answer for \(v\) to (shutdown day of \(u\)) \(+ 1\) and push \(v\) into the queue.
- Repeat until the queue is empty, then output the answer for each station.
Even for stations whose shutdown day has already been determined, it is safe to subtract incoming provisions from their totals. As long as they are not pushed into the queue again, the effects on other stations remain unchanged.
Complexity
Let \(N\) be the number of broadcasting stations and \(M\) be the number of provision relationships.
The time complexity is \(O(N+M)\). Each station is pushed into the queue at most once, and each provision relationship is processed at most once.
The space complexity is \(O(N+M)\) to store the adjacency list, the totals and shutdown days for each station, and the queue.
Implementation Notes
The total amount of materials can reach up to \(M\times10^9\leq2\times10^{14}\), so it must be stored in signed 64-bit integers. Since a station can continue operating if its received amount is equal to the required amount, the shutdown condition is a strict inequality. Stations with a required amount of \(0\) will never shut down. Self-provisions and duplicate provision relationships can also be handled naturally by adding and subtracting each input relationship one by one.
Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
if (!(cin >> N >> M)) return 0;
vector<long long> B(N), available(N, 0);
for (auto &x : B) cin >> x;
vector<vector<pair<int, long long>>> outgoing(N);
for (int i = 0; i < M; ++i) {
int u, v;
long long w;
cin >> u >> v >> w;
--u; --v;
outgoing[u].push_back({v, w});
available[v] += w;
}
vector<int> day(N, 0);
queue<int> q;
for (int v = 0; v < N; ++v) {
if (available[v] < B[v]) {
day[v] = 1;
q.push(v);
}
}
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto [v, w] : outgoing[u]) {
available[v] -= w;
if (day[v] == 0 && available[v] < B[v]) {
day[v] = day[u] + 1;
q.push(v);
}
}
}
for (int v = 0; v < N; ++v) {
if (v) cout << ' ';
cout << day[v];
}
cout << '\n';
}
This editorial was generated by imported.
posted:
last update: