D - Portable Gate Editorial by evima
When moving the gate, it can be considered to move simultaneously with the piece. Additionally, we will always perform operation 1 as needed and consider the following three operations:
- Operation A: When the piece and the gate are on the same vertex, move both the piece and the gate to an adjacent vertex (considered as moving along the edge).
- Operation B: Move the piece to an adjacent vertex (considered as moving along the edge).
- Operation C: Move the piece to the vertex where the gate is located (considered as jumping without moving along edges).
Operations A and B each incur a cost of 1.
While performing operation A, both the piece and the gate move together. However, once operation B is performed, the gate remains in its position until operation C is performed, while the piece moves alone by operation B. Here, it is inefficient to move the piece to the position of the gate using operation B, so we will not consider this.
Let \(v\) be the vertex where the piece and the gate are initially placed. By performing operation C at the end, we assume that the process can only end when the piece and the gate are on the same vertex.
Label the vertices where the gate has been and the edges where the gate has moved with A, and label the other vertices and edges with B. The vertices and edges labeled A form a connected subgraph, specifically a subtree. We will call this subtree \(A\). Vertex \(v\) is included in subtree \(A\).
Additionally, edges labeled B that have one endpoint labeled A are called boundary edges.
Vertices labeled B and edges labeled B that are not boundary edges are divided into several connected components. Each connected component forms a tree and is connected to a vertex in subtree \(A\) by a boundary edge. Let the \(i\)-th connected component be connected to vertex \(l_i\) in subtree \(A\) by boundary edge \(e_i\), and let \(B_i\) be the \(i\)-th connected component with \(e_i\) and \(l_i\) added (which also forms a subtree).
The following three points are important:
- In the optimal operations, it is acceptable to assume that edges labeled A are not traversed by operation B. This is because, by appropriately decomposing, rearranging, and replacing operations, the cost does not increase if the operations are performed in the following manner:
- The piece and the gate start from vertex \(v\), visit all vertices in subtree \(A\) using operation A, and return to vertex \(v\). This is called the journey in subtree \(A\).
- If vertex \(l_i\) is visited during the journey in subtree \(A\), the journey in subtree \(A\) is interrupted. The piece alone starts from vertex \(l_i\), visits all vertices in subtree \(B_i\) using operation B, and returns to vertex \(l_i\). This is called the journey in subtree \(B_i\). After that, the journey in subtree \(A\) is resumed.
- Note that during the journey of subtree \(B_i\), the gate remains at vertex \(l_i\).
- In the optimal operations, it is acceptable to assume that moving along boundary edge \(e_i\) from \(l_i\) (which inevitably involves operation B) occurs only once. This is because if it occurs more than once, moving with the gate using operation A during the first move and returning to \(l_i\) with the gate using operation A at the end does not increase the cost.
- From this, in the journey in subtree \(B_i\), operation C is performed only once at the end.
- In the journeys in subtrees \(A\) and \(B_i\), the farthest vertex from the starting vertex should be visited last.
- That is, if the distance from the starting vertex to the farthest vertex is \(d\) and the number of edges in the subtree is \(m\), the cost of the journey in the subtree is \(2m - d\).
In the final point, the total number of edges in the subtrees is always \(N-1\), so minimizing the cost can be rephrased as maximizing the total sum of \(d\).
Based on these points, the answer for a fixed \(v\) can be calculated using tree DP. When taking \(v\) as the root, the following information should be maintained for the subtree rooted at \(u\):
- The maximum possible distance to the vertex where the journey of subtree \(B_i\) ends, assuming \(u = l_i\).
- The maximum sum of \(d\) if \(u\) is in subtree \(A\) and the journey of subtree \(A\) ends in a vertex in the subtree rooted at \(u\).
- The maximum sum of \(d\) if \(u\) is in subtree \(A\) and the journey of subtree \(A\) ends in a vertex not in the subtree rooted at \(u\).
This calculation can be easily extended to rerooting DP. This allows us to examine all possible starting vertices for the journey. The time complexity is \(\mathrm{O}(N)\).
posted:
last update: