E - 循環搬送網の回収口 / Collection Port of a Circular Conveyor Network Editorial by admin Editorial for printing
importedEditorial
Let the transfer platforms be vertices and the movements to destinations be directed edges. Since each vertex has an out-degree of exactly \(1\), each connected component (ignoring edge directions) consists of a single directed cycle and trees directed toward that cycle. Self-loops are also treated as cycles of length \(1\).
Restricting the Collection Point to Cycles
Suppose a vertex \(x\) not contained in a cycle is chosen as the collection point. Let \(c\) be the first vertex on a cycle reached by following the destinations starting from \(x\). The luggage that reaches \(x\) will still be collected even if the collection point is changed to \(c\), and their number of moves increases by the positive distance from \(x\) to \(c\). Furthermore, the contribution of any newly collected luggage is also non-negative. Therefore, the score of \(c\) is greater than or equal to that of \(x\), so we only need to search for the optimal collection point on the cycles. Since luggage from other connected components is never collected, the answer is the maximum of the maximum scores across all components.
Aggregating Luggage on Trees
We iteratively remove vertices with an in-degree of \(0\) using a queue. For each vertex \(v\), let \(W_v\) be the aggregated amount of luggage, and let \(D_v\) be the sum of the number of moves required for them to reach \(v\). Initially, set \(W_v=A_v\) and \(D_v=0\).
When removing vertex \(u\), letting its destination be \(v\), we update as follows:
\[ D_v \gets D_v+D_u+W_u,\qquad W_v\gets W_v+W_u. \]
This is because each piece of luggage aggregated at \(u\) moves one additional time to reach \(v\). When a vertex’s in-degree decreases to \(0\), we add it to the queue. The vertices remaining after this elimination process are precisely the vertices on the cycles, and \(W_v, D_v\) represent the total amount of luggage whose entry point into the cycle is \(v\), and the sum of moves taken to reach there, respectively. This invariant holds inductively with respect to the elimination order.
Moving the Collection Point along the Cycle
Let the vertices of a cycle along the direction of edges be \(c_0, c_1, \ldots, c_{L-1}\). The score when \(c_0\) is chosen as the collection point is
\[ F_0=\sum_{j=0}^{L-1}D_{c_j}+\sum_{j=1}^{L-1}(L-j)W_{c_j}. \]
We count the moves by separating them into moves before entering the cycle and moves after entering the cycle toward the collection point. Let \(T=\sum_j W_{c_j}\).
When moving the collection point from \(c_{j-1}\) to \(c_j\), for all luggage except those that initially enter at \(c_j\), their number of moves increases by \(1\). On the other hand, for luggage that initially enters at \(c_j\), their moves on the cycle change from \(L-1\) to \(0\). Therefore,
\[ F_j=F_{j-1}+T-LW_{c_j}. \]
This allows us to evaluate all candidate points on a single cycle in \(O(L)\) time. When the length is \(1\), the initial formula also yields \(D_{c_0}\), and no additional updates are needed.
In the actual implementation, the in-degree array can also serve as a marker for remaining vertices; by setting the value to \(0\) once a cycle is traversed, we avoid double-processing. Using the formula above for each cycle, we output the maximum score obtained across all cycles. By eliminating candidates outside cycles, preserving the invariants on tree parts, and using the transition formula on cycles, this correctly finds the maximum among all candidates.
Complexity and Integer Types
Since each vertex is processed a constant number of times, both the time and space complexity are \(O(N)\). No recursion is used.
Since collected luggage is collected before visiting the same platform twice, the number of moves is at most \(N-1\). The score is at most \(N(N-1)\times 10^6<4\times 10^{16}\), which fits within a signed \(64\)-bit integer. Products involving \(W, D, T, F\), and cycle lengths should be computed using this type.
Solution for Independent Verification
From each starting vertex, directly trace the destinations until a vertex appears again. For each newly visited vertex \(v\), add the product of the number of moves taken so far and the amount of luggage at the starting vertex to the score of \(v\). The maximum value after processing all starting vertices is the answer. Without aggregating onto cycles or using transition formulas along cycles, this directly computes the first arrivals as stated in the problem description. This takes \(O(N^2)\) time and \(O(N)\) auxiliary space; although it is naturally slow for large inputs, it has no restrictions on size or recursion limits.
posted:
last update: