E - 三本先を切り離す閉鎖 / Closure Separating Three Lines Ahead Editorial by admin Editorial for printing
importedOverview
Find the minimum cost to close vertices of a tree such that in each remaining connected component, any two vertices can be connected by at most two edges.
Analysis
If we try all subsets of vertices to close, there are \(2^N\) candidates. However, by summarizing whether the root of each subtree is closed, and if kept, what the maximum reachable distance from it is, we can compute the answer using a constant number of states.
The graph of usable edges is a forest. The condition to satisfy is that the diameter of each connected component is at most \(2\). We can perform tree dynamic programming by designating vertex \(1\) as the root.
For the subtree rooted at vertex \(u\), we maintain the following four minimum costs. In every state, all connected components within the subtree must have a diameter of at most \(2\).
- \(D_u\): Close \(u\).
- \(H_{u,0}\): Keep \(u\), and the maximum distance from \(u\) within \(u\)’s connected component is \(0\).
- \(H_{u,1}\): The same maximum distance is \(1\).
- \(H_{u,2}\): The same maximum distance is \(2\).
The value of an impossible or non-existent state is defined as infinity. In the implementation, these are stored in dp[u][0] through dp[u][3], respectively.
Let \(C\) be the set of children of \(u\). When \(u\) is closed, the subtrees of each child become independent, so:
\[ D_u=A_u+\sum_{v\in C}\min(D_v,H_{v,0},H_{v,1},H_{v,2}) \]
Letting \(B=\sum_{v\in C}D_v\), we have \(H_{u,0}=B\).
When the height is \(1\), each child must either be closed or kept with height \(0\). However, at least one child must be kept. We first independently choose the cheaper option for each child, and then add the minimum additional cost needed to ensure that at least one child is kept:
\[ H_{u,1}=\sum_{v\in C}\min(D_v,H_{v,0})+ \min_{v\in C}\bigl(H_{v,0}-\min(D_v,H_{v,0})\bigr) \]
If keeping a child was already the minimal choice, the additional cost is \(0\). If \(u\) has no children, this state cannot exist.
When the height is \(2\), exactly one child must be kept with height \(1\). If any other child were kept as well, the distance from a vertex below the height-\(1\) child to that other child would become \(3\) or more. Therefore,
\[ H_{u,2}=B+\min_{v\in C}(H_{v,1}-D_v) \]
If there is no child for which the height-\(1\) state exists, this state also cannot exist. Connecting a child of height \(2\) to the parent is impossible, as the distance from the parent would become \(3\).
Each formula accounts for all valid choices for the corresponding state, and the costs from different children’s subtrees are summed up. Assuming the children’s states are correct, the transitions above yield the optimal values. Computing inductively from the leaves ensures that all states are correct, and the minimum among the four states at the root is the final answer.
Algorithm
Find the parent of each vertex and a traversal order where parents appear before their children. Then, iterate through the vertices in the reverse of this order, aggregating the four states of the children according to the formulas above. Finally, output the minimum of the four states at the root.
Complexity
Let \(N\) be the number of vertices.
By recording the parents and the traversal order in arrays and processing them in reverse order, the computation can be performed without recursion even for deep trees. Since each edge is processed a constant number of times, the time complexity is \(O(N)\) and the space complexity is \(O(N)\). Any finite cost is at most \(N\times10^9\leq2\times10^{14}\), so signed \(64\)-bit integers should be used. Ensure that infinity is properly distinguished from finite values to avoid performing subtractions with non-existent states.
Source Code
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; if(!(cin>>n)) return 0;
vector<ll>a(n); for(auto &x:a)cin>>x;
vector<vector<int>>g(n);
for(int i=1,u,v;i<n;i++){cin>>u>>v;--u;--v;g[u].push_back(v);g[v].push_back(u);}
vector<int>par(n,-1),order{0};
for(int i=0;i<n;i++)for(int v:g[order[i]])if(v!=par[order[i]]){par[v]=order[i];order.push_back(v);}
const ll INF=4000000000000000000LL;
vector<array<ll,4>>dp(n);
for(int k=n-1;k>=0;k--){
int u=order[k]; ll del=a[u],base=0,best1=0,force=INF,extend=INF;
for(int v:g[u])if(par[v]==u){
del+=*min_element(dp[v].begin(),dp[v].end());
base+=dp[v][0];
best1+=min(dp[v][0],dp[v][1]);
force=min(force,dp[v][1]-min(dp[v][0],dp[v][1]));
if(dp[v][2]<INF)extend=min(extend,dp[v][2]-dp[v][0]);
}
dp[u][0]=del; // removed
dp[u][1]=base; // kept, height 0
dp[u][2]=(force==INF?INF:best1+force); // kept, height 1
dp[u][3]=(extend==INF?INF:base+extend); // kept, height 2
}
cout<<*min_element(dp[0].begin(),dp[0].end())<<'\n';
}
This editorial was generated by imported.
posted:
last update: