Official

E - 循環搬送網の回収口 / Collection Port of a Circular Conveyor Network Editorial by admin

claude5.5opus-high

Overview

This problem asks us to find the maximum total number of moves of all collected packages when we choose a single vertex as the collection point in a graph where each conveyor has an out-degree of exactly \(1\) (a functional graph). Each connected component consists of a single cycle and trees directed toward that cycle. Using this structure, we can compute the score for all candidate collection points in \(O(N)\) by aggregating subtree values for tree components and using incremental updates while rotating around cycle components.

Analysis

Graph Structure

Since each vertex has an out-degree of exactly \(1\), each connected component consists of a single cycle and trees (a forest) directed toward vertices on the cycle. Packages move along the edges and will eventually enter the cycle and keep looping around it indefinitely.

Case Analysis by Collection Point Location

Here, we define the following values for each vertex \(u\):

  • \(\mathrm{depth}[u]\): The distance from \(u\) until it reaches the cycle (vertices on the cycle have depth \(0\)).
  • \(\mathrm{root}(u)\): The first vertex on the cycle reached by following edges starting from \(u\).

(1) When the collection point \(v\) is in a tree (not on a cycle)

The only packages that can reach \(v\) are those in the subtree rooted at \(v\) (the set of vertices that flow into \(v\)). The distance from a vertex \(u\) in this subtree to \(v\) is \(\mathrm{depth}[u]-\mathrm{depth}[v]\), so the score is:

\[\sum_{u \in \mathrm{sub}(v)} A_u(\mathrm{depth}[u]-\mathrm{depth}[v]) = \underbrace{\sum A_u\,\mathrm{depth}[u]}_{sd[v]} - \mathrm{depth}[v]\cdot\underbrace{\sum A_u}_{sw[v]}\]

Once we know the subtree sums \(sw[v]\) and \(sd[v]\), this can be computed in \(O(1)\).

(2) When the collection point \(v\) is on a cycle

All packages in the same connected component enter the cycle and eventually reach \(v\). The number of moves for a package starting at vertex \(u\) is:

\[\mathrm{depth}[u] + (\text{distance from } \mathrm{root}(u) \text{ to } v \text{ along the cycle})\]

Let the cycle vertices be ordered as \(c_0, c_1, \dots, c_{L-1}\) (where \(c_{j+1} = P_{c_j}\)). If we denote the sum of packages in the entire tree rooted at cycle vertex \(c_j\) as \(sw[c_j]\), the score when choosing \(c_k\) as the collection point can be written as:

\[\mathrm{totD} + F(k),\quad F(k) = \sum_{j} sw[c_j]\cdot\big((k-j) \bmod L\big)\]

Here, \(\mathrm{totD}=\sum_{u} A_u\,\mathrm{depth}[u]\) is a constant independent of the choice of collection point.

Naive Approach Is Too Slow

Simulating package movements for every candidate collection point would take at least \(O(N^2)\), which results in TLE since \(N = 200000\).

Incremental Updates on Cycles

When advancing the collection point by one step from \(c_{k-1}\) to \(c_k\), the distance from each \(c_j\) changes as follows:

  • For vertices with \(j \ne k\): The distance increases by \(1\).
  • For \(c_k\) itself: The distance changes from \(L-1\) to \(0\) (equivalent to a decrease of \(L\)).

Therefore, we can update in \(O(1)\) using:

\[F(k) = F(k-1) + \mathrm{totW} - L\cdot sw[c_k]\]

(where \(\mathrm{totW}\) is the total number of packages in the entire component).

Example: Consider a cycle of length \(L=3\) with \(sw = (2, 5, 1)\). - \(F(0) = 2\cdot 0 + 5\cdot 2 + 1\cdot 1 = 11\) - \(F(1) = 11 + 8 - 3\cdot5 = 4\). Direct calculation also yields \(2\cdot1+5\cdot0+1\cdot2=4\), which matches.

Algorithm

  1. Cycle Detection (Topological Sort) Push all vertices with in-degree \(0\) into a queue. Each time a vertex is popped, decrement the in-degree of its destination (Kahn’s algorithm). Vertices remaining at the end belong to cycles. The popped order order represents the order from the leaf side toward the cycle side.
  2. Depth Computation Traverse order in reverse and set \(\mathrm{depth}[v] = \mathrm{depth}[P_v] + 1\). In reverse order, \(\mathrm{depth}[P_v]\) is guaranteed to be finalized first. The depth of vertices on cycles is \(0\).
  3. Subtree Aggregation Initialize \(sw[v]=A_v\) and \(sd[v]=A_v\cdot\mathrm{depth}[v]\). In the forward order of order (from leaves inward), add to the parent: \(sw[P_v] \mathrel{+}= sw[v]\) and \(sd[P_v] \mathrel{+}= sd[v]\).
  4. Tree Candidate Vertices For each vertex \(v\) not on a cycle, update the answer with \(sd[v] - \mathrm{depth}[v]\cdot sw[v]\).
  5. Cycle Candidate Vertices For each cycle, compute \(\mathrm{totD}=\sum sd[c_j]\) and \(\mathrm{totW}=\sum sw[c_j]\). After directly calculating \(F(0)\), compute \(F(1),\dots,F(L-1)\) via incremental updates, and update the answer with \(\mathrm{totD}+F(k)\).

Complexity

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

Key Implementation Points

  • Beware of integer overflow: The score can be up to approximately \(N \cdot A_{\max} \cdot N = 2\times10^5 \cdot 10^6 \cdot 2\times10^5 = 4\times10^{16}\), so use long long.

  • Order of depth computation and aggregation: Compute depth in the reverse topological order (from cycle outward), and aggregate subtree sums in the forward topological order (from leaves toward cycle).

  • Ordering the cycle: If the cycle is ordered along the direction of edges \(c_{j+1}=P_{c_j}\), the distance from \(c_j\) to \(c_k\) is \((k-j) \bmod L\), allowing the incremental update formula to be applied directly.

  • Conversion to 0-indexed: The input \(P_i\) is 1-indexed, so subtract \(1\) when reading the input.

    Source Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int n;
    if(scanf("%d",&n)!=1) return 0;
    vector<int> p(n);
    for(auto&x:p){scanf("%d",&x);x--;}
    vector<ll> a(n);
    for(auto&x:a)scanf("%lld",&x);
    vector<int> indeg(n,0);
    for(int i=0;i<n;i++)indeg[p[i]]++;
    vector<int> order;order.reserve(n);
    vector<char> oncyc(n,1);
    queue<int> q;
    for(int i=0;i<n;i++)if(indeg[i]==0)q.push(i);
    while(!q.empty()){
        int v=q.front();q.pop();
        oncyc[v]=0;order.push_back(v);
        if(--indeg[p[v]]==0)q.push(p[v]);
    }
    vector<ll> depth(n,0);
    for(int k=(int)order.size()-1;k>=0;k--){
        int v=order[k];
        depth[v]=depth[p[v]]+1;
    }
    vector<ll> sw(n),sd(n);
    for(int i=0;i<n;i++){sw[i]=a[i];sd[i]=a[i]*depth[i];}
    for(int v:order){
        sw[p[v]]+=sw[v];
        sd[p[v]]+=sd[v];
    }
    ll ans=0;
    for(int v:order){
        ans=max(ans,sd[v]-depth[v]*sw[v]);
    }
    vector<char> vis(n,0);
    for(int i=0;i<n;i++){
        if(!oncyc[i]||vis[i])continue;
        vector<int> cyc;
        int c=i;
        while(!vis[c]){vis[c]=1;cyc.push_back(c);c=p[c];}
        ll L=cyc.size();
        ll totD=0,totW=0;
        for(int x:cyc){totD+=sd[x];totW+=sw[x];}
        // F(c0): r at pos j reaches c0 in (0-j) mod L steps
        ll F=0;
        for(ll j=0;j<L;j++){
            ll d=(L-j)%L;
            F+=sw[cyc[j]]*d;
        }
        ans=max(ans,totD+F);
        for(ll j=1;j<L;j++){
            F=F+totW-L*sw[cyc[j]];
            ans=max(ans,totD+F);
        }
    }
    printf("%lld\n",ans);
}

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