D - 接続規格と変換器 / Connection Standards and Converters Editorial by admin Editorial for printing
claude5.5opus-highOverview
This problem asks us to assign either \(0\) or \(1\) to each vertex of a tree to minimize the sum of the “assignment cost for each vertex” and the “cost for edges whose endpoints have different values”. This can be solved using tree DP.
Analysis
- Naive approach and its limitations: Exhaustively searching all assignments for every vertex gives \(2^N\) possibilities. This will not run in time at all for \(N \le 200000\).
- Key observation: The cost is determined solely by the “individual vertex costs” and the “costs based on the relationship between both endpoints of an edge”. Therefore, once the standard for a vertex \(v\) is fixed, the optimal choices for the descendants of \(v\) can be decided independently of the rest of the tree.
- In a tree, the only connection between the subtree of \(v\) and the outside is the single edge connecting \(v\) to its parent.
- The cost of that edge depends only on the standards assigned to \(v\) and its parent.
- DP definition: We define the DP table as follows:
\(dp[v][s]\) = the minimum cost within the subtree of vertex \(v\) (the total cost of equipment and edges in the subtree) when standard \(s\) is assigned to vertex \(v\) - Transitions: For each child \(w\) (with edge cost \(c\)), we choose whether to assign \(w\) the same standard as its parent, or a different standard and pay the converter cost \(c\). $\(dp[v][s] = (\text{cost of setting standard } s) + \sum_{w \in \text{children}} \min\bigl(dp[w][s],\ dp[w][1-s] + c\bigr)\)\( - **Answer**: Taking vertex \)1\( as the root, the answer is \)\min(dp[\text{root}][0], dp[\text{root}][1])$.
Concrete example: Consider the case where vertex \(1\) \((A=5,B=0)\) and vertex \(2\) \((A=0,B=5)\) are connected by an edge with cost \(C=3\). - \(dp[2] = (0, 5)\). - \(dp[1][0] = 5 + \min(0,\ 5+3) = 5\). - \(dp[1][1] = 0 + \min(5,\ 0+3) = 3\). - The answer is \(3\). It is optimal to assign standard \(1\) to vertex \(1\), standard \(0\) to vertex \(2\), and install a converter.
Algorithm
- Initialize \(dp[i][0] = A_i\) and \(dp[i][1] = B_i\).
- Starting from the root (vertex \(1\)), construct the visiting order
orderusing a stack-based DFS. At the same time, record the parentparof each vertex and the edge cost to its parentpc. - Traverse
orderin reverse order. This ensures that children are always processed before their parents. For each vertex \(v\) (except the root), add the following values to its parent \(p\): $\(dp[p][s] \mathrel{+}= \min(dp[v][s],\ dp[v][1-s] + pc[v]) \quad (s = 0, 1)\)$ - Output \(\min(dp[0][0], dp[0][1])\).
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
Implementation Notes
Avoid recursion: Since \(N\) can be up to \(200000\), if the tree is a line (path), a recursive DFS risks causing a stack overflow. By using a stack to generate the DFS visiting order and processing it in reverse, we can compute the DP in “child \(\to\) parent” order without recursion.
Preventing overflow: The total cost can reach up to approximately \(10^9 \times 2 \times 10^5 \times 2 = 4\times 10^{14}\), which does not fit in a 32-bit integer. Use a 64-bit integer type such as
long long.Case where \(N=1\): There are no input edges. The loop simply does not execute, and \(\min(A_1, B_1)\) is correctly output.
Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
if(scanf("%d",&n)!=1) return 0;
vector<array<long long,2>> dp(n);
for(int i=0;i<n;i++){
long long a,b;
scanf("%lld %lld",&a,&b);
dp[i][0]=a;dp[i][1]=b;
}
vector<vector<pair<int,long long>>> g(n);
for(int j=0;j<n-1;j++){
int u,v;long long c;
scanf("%d %d %lld",&u,&v,&c);
u--;v--;
g[u].push_back({v,c});
g[v].push_back({u,c});
}
vector<int> order;order.reserve(n);
vector<int> par(n,-1);
vector<long long> pc(n,0);
vector<int> st;st.push_back(0);par[0]=0;
vector<char> vis(n,0);vis[0]=1;
while(!st.empty()){
int v=st.back();st.pop_back();
order.push_back(v);
for(auto [w,c]:g[v]){
if(!vis[w]){
vis[w]=1;par[w]=v;pc[w]=c;st.push_back(w);
}
}
}
for(int i=n-1;i>=1;i--){
int v=order[i];
int p=par[v];
long long c=pc[v];
for(int s=0;s<2;s++){
dp[p][s]+=min(dp[v][s],dp[v][1-s]+c);
}
}
printf("%lld\n",min(dp[0][0],dp[0][1]));
}
This editorial was generated by claude5.5opus-high.
posted:
last update: