Official

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

claude5.5opus-high

Overview

We classify each vertex of a tree as either having a relief station or not. For each vertex without a relief station, at least \(2\) of its adjacent vertices must have one. We want to find a selection that satisfies this condition with the minimum total cost. We can solve this problem using tree DP.

Analysis

Problem with the Naive Approach

Trying all combinations of whether to place a relief station at each village results in \(2^N\) configurations. With \(N \le 200000\), this will by no means run in time.

Key Observation

Consider only the subtree rooted at vertex \(v\). When we do not place a relief station at \(v\), whether the condition for \(v\) is satisfied is determined by the following two factors:

  • How many children of \(v\) have relief stations
  • Whether the parent of \(v\) has a relief station

Since the parent lies outside the subtree, we cannot know its status during the computation of the subtree. Therefore, we maintain the number of chosen children \(k\) as a state and check it together with the parent’s information when processing the parent.

The adjacent vertices of \(v\) are its children and its single parent. Therefore, if \(v\) does not have a relief station:

  • If \(2\) or more children are chosen, the condition is satisfied regardless of the parent.
  • If \(1\) child is chosen, the condition is satisfied if the parent is chosen.
  • If \(0\) children are chosen, even if the parent is chosen, there is only \(1\) adjacent relief station, so the condition is not satisfied.

DP State

For the subtree rooted at vertex \(v\), assuming that the conditions for all vertices other than \(v\) are satisfied, we define the minimum costs as follows:

  • \(f_1[v]\): The minimum cost when placing a relief station at \(v\).
  • \(g_k[v]\): The minimum cost when not placing a relief station at \(v\), and exactly \(k\) children are chosen (\(k=2\) represents “\(2\) or more”).

Transitions

When choosing \(v\) (\(f_1[v]\)) From the perspective of child \(c\), the parent \(v\) provides a relief station. Therefore, child \(c\) can be in any of the following states:

  • Chosen: \(f_1[c]\)
  • Not chosen, with \(1\) chosen child: \(g_1[c]\) (\(2\) in total when combined with parent \(v\))
  • Not chosen, with \(2\) or more chosen children: \(g_2[c]\)

Thus, we obtain the following formula: $\(f_1[v] = A_v + \sum_{c} \min(f_1[c], g_1[c], g_2[c])\)$

When not choosing \(v\) (\(g_k[v]\)) From the perspective of child \(c\), the parent \(v\) does not have a relief station. Child \(c\) has two choices:

  • Choose \(c\) (cost \(f_1[c]\)). In this case, the number of chosen children of \(v\) increases by \(1\).
  • Do not choose \(c\). In this case, \(c\) requires \(2\) or more relief stations from its own children alone, so the cost is \(g_2[c]\). The count does not increase.

We process the children one by one, updating the count \(k \in \{0,1,2\}\) in a knapsack-like manner (capping counts of \(2\) or more at \(2\)).

Answer

The root has no parent. Therefore, if the root is not chosen, it requires \(2\) or more chosen children. $\(\text{Answer} = \min(f_1[\text{root}], g_2[\text{root}])\)$

Concrete Example

Consider a star graph with center \(1\) connected to leaves \(2,3,4\), and \(A = (10, 1, 1, 1)\).

  • Values for leaf \(c\): Since it has no children, \(f_1[c]=1\), \(g_0[c]=0\), and \(g_1[c]=g_2[c]=\infty\).
  • \(f_1[1]\): \(10 + 3\times\min(1,\infty,\infty) = 13\).
  • \(g_2[1]\): If vertex \(1\) is not chosen, the leaves must all be chosen because not choosing them yields \(g_2=\infty\). The cost is \(3\), and since \(k=3\) is capped at \(2\), \(g_2[1]=3\).

The answer is \(\min(13,3)=3\).

Algorithm

  1. Perform a DFS rooted at vertex \(1\) to find the visit order order and parent array par.
  2. Traverse order in reverse order (bottom-up from the leaves), and for each vertex \(v\), perform the following:
    • Initialize sel to \(A_v\), and prepare an array d[3] = {0, ∞, ∞}.
    • For each child \(c\), perform the following two steps:
      • Add \(\min(f_1[c],g_1[c],g_2[c])\) to sel.
      • Update d:
           - Choose $c$: `nd[min(2,k+1)] = min(…, d[k] + f1[c])`
           - Do not choose $c$: `nd[k] = min(…, d[k] + g2[c])`
        
    • Set \(f_1[v]=\) sel and \(g_k[v]=\) d[k].
  3. Output \(\min(f_1[\text{root}], g_2[\text{root}])\).

When \(N=1\), the root has no children, so \(g_2=\infty\) and the answer is \(A_1\). This corresponds to the fact that “since there are no adjacent villages, a station must be placed at the village itself.”

Complexity

  • Time Complexity: \(O(N)\) (each edge is processed a constant number of times with \(3\) states)
  • Space Complexity: \(O(N)\)

Implementation Notes

  • Avoid recursion: Since \(N\) can be up to \(200000\), a recursive DFS risks causing a stack overflow if the tree is path-like. Instead, find the visit order using an explicit stack, and compute the DP in reverse order.

  • Handling \(\infty\): Represent impossible states with INF. To prevent overflow and incorrect comparisons, use a helper function ad that clamps values to INF if an addition exceeds INF.

  • Cap the count at \(2\): Since the condition only depends on distinguishing between “\(0\), \(1\), and \(2\) or more”, \(3\) states are sufficient.

  • Where to use \(g_1\): \(g_1[c]\) is valid only when the parent is chosen. Note that in transitions where the parent is not chosen, if a child is not chosen, \(g_2[c]\) must be used. Although \(g_0\) is never directly referenced, it is necessary as an intermediate state in the DP.

  • Use 64-bit integers, as the total cost can reach up to approximately \(2\times10^{14}\).

    Source Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=(ll)1e18;
ll ad(ll a,ll b){ll s=a+b; return s>INF?INF:s;}
int main(){
    int n; scanf("%d",&n);
    vector<ll>A(n);
    for(auto&x:A)scanf("%lld",&x);
    vector<vector<int>>g(n);
    for(int i=0;i<n-1;i++){int u,v;scanf("%d %d",&u,&v);u--;v--;g[u].push_back(v);g[v].push_back(u);}
    vector<int>order,par(n,-1);
    order.reserve(n);
    vector<int>st={0};par[0]=-1;
    vector<char>vis(n,0);vis[0]=1;
    while(!st.empty()){
        int v=st.back();st.pop_back();order.push_back(v);
        for(int c:g[v]) if(!vis[c]){vis[c]=1;par[c]=v;st.push_back(c);}
    }
    vector<ll> f1(n),g0(n),g1(n),g2(n);
    for(int i=n-1;i>=0;i--){
        int v=order[i];
        ll sel=A[v];
        ll d[3]={0,INF,INF};
        for(int c:g[v]) if(c!=par[v]){
            ll opt=min({f1[c],g1[c],g2[c]});
            sel=ad(sel,opt);
            ll nd[3]={INF,INF,INF};
            for(int k=0;k<3;k++){
                if(d[k]>=INF)continue;
                // child selected
                int nk=min(2,k+1);
                nd[nk]=min(nd[nk],ad(d[k],f1[c]));
                // child unselected needs g2
                nd[k]=min(nd[k],ad(d[k],g2[c]));
            }
            for(int k=0;k<3;k++)d[k]=nd[k];
        }
        f1[v]=sel;g0[v]=d[0];g1[v]=d[1];g2[v]=d[2];
    }
    ll ans=min(f1[0],g2[0]);
    printf("%lld\n",ans);
}

This editorial was generated by claude5.5opus-high.

posted:
last update: