Official

E - 二方向から支える救護所 / Relief Stations Supporting from Two Directions Editorial by admin Editorial for printing

imported

Overview

Find the minimum cost to establish aid stations in villages connected by a tree such that every village without an aid station has at least two adjacent aid stations.

Analysis

Trying all subsets of villages to place aid stations yields \(2^N\) candidates. If we include whether the parent has an aid station in the state, we can combine the optimal values for each child’s subtree.

Consider the tree rooted at village \(1\). When a village does not have an aid station, the required number of aid stations among its children depends on whether its parent has an aid station.

For each vertex \(v\), we maintain the minimum cost such that all conditions within its descendants are satisfied, using the following three states:

  • \(S_v\): An aid station is placed at \(v\) itself.
  • \(P_v\): No aid station is placed at \(v\), assuming that its parent has an aid station. At least one child must have an aid station.
  • \(Q_v\): Neither \(v\) nor its parent has an aid station. At least two children must have aid stations.

Values for unreachable states are set to infinity. First, if \(v\) has an aid station, we can choose either \(S_u\) or \(P_u\) as the state for each child \(u\), so:

\[ S_v=A_v+\sum_{u:\text{child}}\min(S_u,P_u) \]

When \(v\) does not have an aid station, the options for each child \(u\) are \(S_u\) or \(Q_u\). We use a small dynamic programming approach to process the children sequentially. Let the array \(d_0,d_1,d_2\) represent the minimum costs when the number of children processed so far that have an aid station is zero, one, and two or more, respectively. Initially, this is set to \((0,\infty,\infty)\).

Choosing \(Q_u\) for child \(u\) adds cost \(Q_u\) without changing the count category. Choosing \(S_u\) increments the count category by one (capping at \(2\) for two or more) and adds cost \(S_u\). After processing all children, \(P_v=\min(d_1,d_2)\) and \(Q_v=d_2\).

Since the root has no parent, the answer is \(\min(S_1,Q_1)\).

Correctness

When a leaf does not have an aid station, even if its parent has one, it has at most one adjacent aid station, so the condition cannot be satisfied. With the initialization above, \(P_v,Q_v\) become infinity, and only \(S_v=A_v\) is feasible.

Assume that the three states for the children have been computed correctly. When \(v\) has an aid station, the states each child can choose are precisely \(S_u,P_u\), and since there are no paths between the subtrees of different children, we can choose the minimum value independently for each. Similarly, when \(v\) does not have an aid station, the choices for the children are precisely \(S_u,Q_u\). The three-element array considers all of these choices and ultimately adopts only those choices that satisfy the required number of children with aid stations. Therefore, all three states correctly represent the minimum costs. By induction from the leaves, this holds at the root as well, and the answer reflecting that the root has no parent is optimal.

Algorithm

Find a vertex sequence where parents appear before their children along with each vertex’s parent, and compute the above three states in reverse order of this vertex sequence. For each child, update the three-element array, and output \(\min(S_1,Q_1)\) at the root.

Complexity

Let \(N\) be the number of villages.

Since we only update a constant number of states for each child, the time complexity is \(O(N)\) and the space complexity is \(O(N)\). In this implementation, by constructing a vertex sequence where parents appear before children and processing it in reverse order, recursive calls are avoided even for long chains.

Implementation Notes

Any feasible cost is at most \(N\cdot 10^9\leq 2\times10^{14}\). Use signed \(64\)-bit integers and set infinity to \(4\times10^{18}\). Exclude states containing infinity from addition. Since \(S_u\) is always finite, the addition for \(S_v\) also handles only finite costs.

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>p(n,-1),order{0};p[0]=0;
 for(int i=0;i<n;i++){int v=order[i];for(int u:g[v])if(u!=p[v]){p[u]=v;order.push_back(u);}}
 const ll INF=4000000000000000000LL;
 vector<ll>s(n),one(n),two(n);
 for(int i=n-1;i>=0;i--){int v=order[i];s[v]=a[v];array<ll,3>d={0,INF,INF};
  for(int u:g[v])if(p[u]==v){
   s[v]+=min(s[u],one[u]);array<ll,3>e={INF,INF,INF};
   for(int k=0;k<3;k++)if(d[k]<INF){
    if(two[u]<INF)e[k]=min(e[k],d[k]+two[u]);
    e[min(2,k+1)]=min(e[min(2,k+1)],d[k]+s[u]);
   }d=e;
  }
  one[v]=min(d[1],d[2]);two[v]=d[2];
 }
 cout<<min(s[0],two[0])<<'\n';
}

This editorial was generated by imported.

posted:
last update: