Official

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

claude5.5opus-high

Overview

The total amount of material each broadcasting station receives decreases whenever other stations shut down. We can simulate this chain reaction of shutdowns using a day-by-day leveled BFS (breadth-first search) to determine the shutdown day for each station.

Analysis

Key Observation 1: Received Material Only Decreases Over Time

Since a station never reopens once it shuts down, the set of operating stations only shrinks as the days progress. Therefore, the total amount of material \(S_v\) that station \(v\) receives on any given morning (the sum of supplies from operating stations) is monotonically decreasing.

Station \(v\) shuts down on the night of the day when \(S_v < B_v\), and \(S_v \ge B_v\) holds on all preceding days. In other words, it is sufficient to determine when \(S_v\) falls below \(B_v\) for the first time.

Key Observation 2: The Effect of a Shutdown Appears the Next Morning

A station that shuts down on the night of day \(d\) still provides material on the morning of day \(d\). It stops providing material starting from the morning of day \(d+1\). Therefore:

  • Remove all outgoing edges from the stations that shut down on the night of day \(d\).
  • Stations \(v\) for which \(S_v < B_v\) as a result will shut down on the night of day \(d+1\).

Following this rule, we can process the simulation day by day.

Let’s trace this with a concrete example. Suppose there are edges \(1 \to 2\) (\(W=5\)), \(2 \to 3\) (\(W=3\)), and \(B = (1, 5, 3)\). - Morning of Day 1: \(S = (0, 5, 3)\). Since \(S_1 = 0 < 1\), station 1 shuts down on the night of Day 1. - Morning of Day 2: Supplies from station 1 cease, so \(S_2 = 0 < 5\). Station 2 shuts down on the night of Day 2. - Morning of Day 3: \(S_3 = 0 < 3\). Station 3 shuts down on the night of Day 3.

The answer is 1 2 3.

Drawback of the Naive Approach

Recalculating the material received by every station each day takes \(O(N+M)\) per day. Since the chain of shutdowns can last up to \(N\) days, the overall time complexity would be \(O(N(N+M))\), leading to TLE.

Solution

Changes occur only at the destinations of outgoing edges from newly shut-down stations. Therefore, we can update \(S_v\) incrementally. Since each station shuts down at most once, each edge is processed at most once.

Algorithm

  1. Compute the total received material \(S_v = \sum_{(u \to v)} W\) under the initial state where all stations are operating.
  2. Stations satisfying \(S_v < B_v\) are the ones shutting down on Day 1; set \(\text{ans}_v = 1\) and add them to a list cur.
  3. While cur is not empty, repeat the following (let the current day be \(d\)):
    • For each station \(u\) in cur, examine its outgoing edges \(u \to v\) (with weight \(W\)).
    • If \(v\) has already shut down, ignore it.
    • Otherwise, update \(S_v \mathrel{-}= W\). If \(S_v < B_v\), set \(\text{ans}_v = d+1\) and add \(v\) to the list nx for the next day.
    • Replace cur with nx, and increment \(d\) by 1.
  4. Stations that never shut down retain \(\text{ans}_v = 0\).

Handling self-loops (\(u \to u\)): Since \(\text{ans}_u\) is already set when \(u\) shuts down, it is naturally skipped during the process. Multiple edges also pose no issue, as the weight is subtracted separately for each edge.

Complexity

  • Time Complexity: \(O(N + M)\) (each station shuts down at most once, and each edge is processed only once when its starting station shuts down)
  • Space Complexity: \(O(N + M)\)

Implementation Notes

  • Since \(S_v\) can be up to \(2 \times 10^5 \times 10^9 = 2 \times 10^{14}\), use a 64-bit integer (long long).

  • The condition is “shut down if less than \(B_v\)”, so the check is S[v] < B[v]. A station with \(B_v = 0\) will never shut down.

  • The shutdown check for Day 1 is performed using the initial state of \(S\) before removing any edges. Dividing the list by day correctly captures the condition that stations “shut down simultaneously on the same night.”

  • Storing the adjacency list using head and nxt arrays improves memory efficiency. Since there is a large volume of I/O, use fast I/O methods.

    Source Code

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,m;
    if(scanf("%d %d",&n,&m)!=2)return 0;
    vector<long long> B(n+1),S(n+1,0);
    for(int i=1;i<=n;i++)scanf("%lld",&B[i]);
    vector<int> head(n+1,-1),nxt(m),to(m);
    vector<long long> w(m);
    for(int j=0;j<m;j++){
        int u,v;long long W;
        scanf("%d %d %lld",&u,&v,&W);
        to[j]=v;w[j]=W;nxt[j]=head[u];head[u]=j;
        S[v]+=W;
    }
    vector<int> ans(n+1,0);
    vector<int> cur,nx;
    for(int i=1;i<=n;i++) if(S[i]<B[i]){ans[i]=1;cur.push_back(i);}
    int d=1;
    while(!cur.empty()){
        nx.clear();
        for(int u:cur){
            for(int e=head[u];e!=-1;e=nxt[e]){
                int v=to[e];
                if(ans[v])continue;
                S[v]-=w[e];
                if(S[v]<B[v]){ans[v]=d+1;nx.push_back(v);}
            }
        }
        swap(cur,nx);
        d++;
    }
    string out;
    out.reserve(n*8);
    for(int i=1;i<=n;i++){
        out+=to_string(ans[i]);
        out+=(i==n?'\n':' ');
    }
    fputs(out.c_str(),stdout);
}

This editorial was generated by claude5.5opus-high.

posted:
last update: