D - 素材が減る放送局 / Broadcasting Station with Dwindling Materials Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
This problem asks us to simulate the cascading shutdown process caused by suspended broadcasting stations no longer providing materials, and to determine on the night of which day each broadcasting station ceases operations (or whether it continues to operate indefinitely).
Analysis
Naive Approach and Its Limitations
What happens if we naively recalculate the total amount of materials supplied from currently operating stations for all stations every day? In the worst case, one station shuts down each day, and the simulation could continue for \(N\) days. If we examine all supply relationships (\(M\) edges) every day, the total time complexity becomes \(O(NM)\). Since the constraints for this problem are \(N, M \leq 200000\), \(NM \approx 4 \times 10^{10}\), which will exceed the time limit and result in TLE (Time Limit Exceeded).
Optimization Using Incremental Updates
We do not need to recalculate the amount of received materials from scratch every day. Instead, we can use the idea of incremental updates: “when a station ceases operations, the amount of material received by each recipient decreases by the amount that station was providing.”
Initial State (Day 1): Since all stations are operating initially, the initial amount of materials \(s[v]\) received by each station \(v\) is the sum of the amounts \(W\) across all incoming supply relationships to station \(v\). Stations that satisfy \(s[v] < B_v\) at this point will cease operations on the night of Day 1.
Cascade on Day 2 and Beyond: When a station \(u\) ceases operations, each station \(v\) that was receiving materials from station \(u\) has its incoming materials reduced by \(W\) (\(s[v] \gets s[v] - W\)). Any station \(v\) that newly satisfies \(s[v] < B_v\) due to this decrease will cease operations on the night of the next day.
Once a station ceases operations, it never resumes. Therefore, each station shuts down at most once, and each supply relationship only needs to be checked once—specifically, at the moment the providing station shuts down. This can be handled efficiently using a queue-based algorithm similar to Breadth-First Search (BFS) or topological sort.
Algorithm
- Initialization:
- Calculate the initial total amount of materials \(s[v]\) received by each station \(v\).
- Find all stations \(v\) satisfying \(s[v] < B_v\), record their shutdown day as \(1\), and push them to the “current shutdown queue”
cur_q. Also, mark them as shut down.
- Simulation via BFS:
- Start with day count
day = 1, and repeat the following untilcur_qis empty:- Prepare a queue
next_qto store stations that will shut down on the next day. - Pop stations \(u\) from
cur_q(stations that shut down on the night of dayday) one by one. - For each station \(v\) to which station \(u\) supplied materials, if station \(v\) is still operating:
- Subtract the supplied amount $W$ from $s[v]$ ($s[v] \gets s[v] - W$). - If $s[v] < B_v$ now holds, station $v$ is confirmed to shut down on the night of day `day + 1`. Mark it as shut down, record its shutdown day as `day + 1`, and push it to `next_q`. - Replace
cur_qwithnext_q, and incrementdayby \(1\).
- Prepare a queue
- Start with day count
- Output Results:
- Output the shutdown day for each station. The shutdown day for stations that never shut down remains at the initial value of \(0\).
Complexity
- Time Complexity: \(O(N + M)\)
- Each station is pushed to the queue at most once, and each supply relationship (directed edge) is examined at most once when the providing station shuts down. Therefore, the algorithm runs in linear time with respect to the number of vertices \(N\) and edges \(M\).
- Space Complexity: \(O(N + M)\)
- Storing the graph using an adjacency list and maintaining the arrays uses \(O(N + M)\) memory.
Implementation Notes
Beware of Integer Overflow:
- The required material amounts \(B_i\) and supply amounts \(W_j\) can be up to \(10^9\). The total received materials \(s[v]\) for a station can reach up to \(M \times \max(W_j) = 2 \times 10^5 \times 10^9 = 2 \times 10^{14}\), which overflows a 32-bit integer. Be sure to use a 64-bit integer type (such as
long longin C++) for \(s[v]\) and \(B_i\).
- The required material amounts \(B_i\) and supply amounts \(W_j\) can be up to \(10^9\). The total received materials \(s[v]\) for a station can reach up to \(M \times \max(W_j) = 2 \times 10^5 \times 10^9 = 2 \times 10^{14}\), which overflows a 32-bit integer. Be sure to use a 64-bit integer type (such as
Self-loops and Multiple Edges:
As stated in the problem statement, self-supplies (\(u_j = v_j\)) and multiple supply relationships between the same pair of stations may exist. However, in this incremental update algorithm, a station that has shut down is already marked as such, and updates/pushes to the queue are only performed for stations that have not yet shut down. Therefore, these cases are handled correctly without requiring any special edge-case handling.
Source Code
#include <iostream>
#include <vector>
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 + 1);
for (int i = 1; i <= n; ++i) {
cin >> b[i];
}
vector<vector<pair<int, long long>>> adj(n + 1);
vector<long long> s(n + 1, 0);
for (int j = 0; j < m; ++j) {
int u, v;
long long w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
s[v] += w;
}
vector<int> ans(n + 1, 0);
vector<bool> stopped(n + 1, false);
vector<int> cur_q;
for (int i = 1; i <= n; ++i) {
if (s[i] < b[i]) {
stopped[i] = true;
ans[i] = 1;
cur_q.push_back(i);
}
}
int day = 1;
while (!cur_q.empty()) {
vector<int> next_q;
for (int u : cur_q) {
for (const auto& edge : adj[u]) {
int v = edge.first;
long long w = edge.second;
if (!stopped[v]) {
s[v] -= w;
if (s[v] < b[v]) {
stopped[v] = true;
ans[v] = day + 1;
next_q.push_back(v);
}
}
}
}
cur_q = move(next_q);
day++;
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << (i == n ? "" : " ");
}
cout << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: