E - 三本先を切り離す閉鎖 / Closure Separating Three Lines Ahead Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We consider the facilities and paths as a tree. The problem asks us to delete vertices such that the diameter of each remaining connected component is at most \(2\). Utilizing the fact that any valid connected component is a “star graph”, we find the minimum closure cost using tree DP.
Analysis
Remaining Connected Components are Star Graphs
In a tree, the path connecting any two vertices is unique. Therefore, the problem’s condition can be rephrased as: “in each remaining connected component, the distance between the two furthest vertices is at most \(2\).”
Such a tree is a star graph, which consists of a single central vertex and vertices directly connected to it. This also includes cases with only \(1\) vertex or only \(1\) edge.
For example, the following structure satisfies the condition:
a
|
b---c---d
On the other hand, in the following structure, the distance between \(a\) and \(d\) is \(3\), so it does not satisfy the condition:
a---b---c---d
In other words, we see that each connected component after closures must form a star graph.
“Whether to Close or Not” Does Not Provide Enough States
Exhaustively searching over all subsets of vertices to close yields \(2^N\) candidates, which will not run in time.
Using tree DP allows us to process each child’s subtree independently, but having only the two states “close the vertex” and “keep the vertex” is insufficient. This is because when keeping a vertex, the conditions for connecting it to its parent or children differ depending on whether the vertex is the center of a star graph or a vertex adjacent to the center.
Therefore, we manage the minimum cost by distinguishing configurations relevant to the connection with the parent.
Algorithm
Consider a rooted tree with vertex \(1\) as the root.
1. DP States
For the subtree of vertex \(v\), we define the following values. In every state, assume that all remaining connected components that do not contain \(v\) satisfy the condition.
| State | Meaning | Corresponding Code Variable |
|---|---|---|
| \(D_v\) | Minimum cost when closing \(v\) | deleted[v] |
| \(L_v\) | Minimum cost when keeping \(v\) and closing all of its children | leaf[v] |
| \(S_v\) | Minimum cost when forming a star graph centered at \(v\) | star[v] |
| \(B_v\) | Minimum cost when isolating \(v\) or connecting it to a star graph centered at exactly \(1\) child | leaf[v] + delta[v] |
In \(L_v\), \(v\) is isolated within its subtree. If the parent is kept, \(v\) can connect to it as a leaf of a star graph centered at the parent.
In \(S_v\), any number of children can be kept. However, the kept children must be leaves of the star graph.
In \(B_v\), we handle the configuration where \(v\) itself is not the center of the star graph, but connects to a center on the child’s side. This also includes the case where \(v\) is isolated.
2. Transitions for Closing the Vertex
When \(v\) is closed, the subtrees of its children become disconnected from each other.
Each child \(u\) can either be closed or kept as part of a star graph, so:
\(D_v = A_v + \sum_{u:\,\text{child of } v} \min(D_u,S_u,B_u)\)
3. Transitions for Closing All Children
Since we keep \(v\) and close all of its children:
\(L_v = \sum_{u:\,\text{child of } v} D_u\)
Vertices further below the children are handled optimally within \(D_u\). This does not mean the entire subtree of each child is closed.
4. Transitions for Making the Current Vertex the Center
When \(v\) is the center, there are \(2\) choices for each child \(u\):
- Close \(u\): cost \(D_u\)
- Keep \(u\) as a leaf: cost \(L_u\)
Therefore:
\(S_v = \sum_{u:\,\text{child of } v} \min(D_u,L_u)\)
If vertices below a kept child were also connected, it would no longer form a star graph centered at \(v\); hence, we use \(L_u\) here.
5. Transitions for Making a Child the Center
First, consider the state where all children are closed. The cost for this is \(L_v\).
If we keep child \(u\) as a center, only for that child do we replace the cost \(D_u\) with \(S_u\). The change in cost is:
\(S_u-D_u\)
Since \(v\) becomes a leaf of the star graph, it can connect to at most \(1\) child. Including the option of not connecting to any child, if we let:
\(\Delta_v = \min\left(0,\min_{u:\,\text{child of } v}(S_u-D_u)\right)\)
then:
\(B_v = L_v+\Delta_v\)
If \(v\) has no children, we set \(\Delta_v=0\).
delta[v] in the code represents this \(\Delta_v\).
6. Computing from Leaves to the Root
We set the initial values as follows:
- \(D_v=A_v\)
- \(L_v=0\)
- \(S_v=0\)
- \(\Delta_v=0\)
We can compute the equations above by updating the parent once the values for its children are determined.
In the code, we construct an order where parents appear before their children, and process it in reverse. The update from child \(v\) to parent \(p\) is as follows:
- Add \(\min(D_v,S_v,B_v)\) to \(D_p\)
- Add \(D_v\) to \(L_p\)
- Add \(\min(D_v,L_v)\) to \(S_p\)
- Update \(\Delta_p\) to \(\min(\Delta_p,S_v-D_v)\)
Finally, the root \(r\) can either be closed or kept in either configuration, so the answer is:
\(\min(D_r,S_r,L_r+\Delta_r)\)
Justification of Correctness
When \(v\) is kept, the connected component containing \(v\) must be a star graph.
Considering the structure within its subtree, it must be one of the following:
- \(v\) itself is the center: handled by \(S_v\)
- One of the children is the center, and \(v\) is its leaf: handled by \(B_v\)
- \(v\) is isolated: included in both \(S_v\) and \(B_v\)
Additionally, the configuration needed when connecting to a star graph centered at the parent is handled by \(L_v\).
Each transition considers all choices that preserve these structures and selects the minimum cost among them. Therefore, computing in order from the leaves yields the overall minimum cost.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(N)\)
Each vertex and edge is processed a constant number of times.
Implementation Notes
Process iteratively without recursion.
If the tree is a line graph, the depth can be up to \(N\). By storing the processing order of vertices in an array and traversing it in reverse (as in the code), we can avoid recursion depth limits.Initialize
deltato \(0\).
This corresponds to the option of “not keeping any child as a center”. Only when there is a negative difference can keeping that child improve the cost.The total cost does not fit in a 32-bit integer.
Since it can reach up to \(2\times 10^{14}\), a 64-bit integer is required in languages with fixed-width integers. Python handles this automatically with its arbitrary-precision integers.\(N=1\) can be handled with the same logic.
Since the single vertex can be kept without being closed, the answer is \(0\).Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
n = data[0]
deleted = data[1:n + 1]
graph = [[] for _ in range(n)]
for i in range(n + 1, len(data), 2):
u = data[i] - 1
v = data[i + 1] - 1
graph[u].append(v)
graph[v].append(u)
del data
parent = [-1] * n
order = [0]
for v in order:
pv = parent[v]
for u in graph[v]:
if u != pv:
parent[u] = v
order.append(u)
del graph
leaf = [0] * n
star = [0] * n
delta = [0] * n
for i in range(n - 1, 0, -1):
v = order[i]
p = parent[v]
dv = deleted[v]
lv = leaf[v]
sv = star[v]
kept = lv + delta[v]
if sv < kept:
kept = sv
deleted[p] += dv if dv < kept else kept
leaf[p] += dv
star[p] += dv if dv < lv else lv
change = sv - dv
if change < delta[p]:
delta[p] = change
print(min(deleted[0], star[0], leaf[0] + delta[0]))
if __name__ == "__main__":
main()
This editorial was generated by gpt-6-astra-medium.
posted:
last update: