E - 二方向から支える救護所 / Relief Stations Supporting from Two Directions 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
For each vertex of the tree, we choose whether to place a relief station on it or not. If we do not place a relief station, it must be supported by relief stations on at least \(2\) adjacent vertices. We want to minimize the total cost.
We treat the tree as a rooted tree and solve this using tree DP, taking into account whether a relief station is placed on the parent vertex.
Analysis
Whether the Parent Has a Relief Station Is Crucial
If we try all subsets of villages to place relief stations on, there are \(2^N\) candidates. This will not run in time for \(N \leq 200000\).
Therefore, we consider a rooted tree with village \(1\) as the root. The neighbors of a vertex \(v\) are partitioned into its parent and its children.
When we do not place a relief station on \(v\), the required number of relief stations on its children is as follows:
- If the parent has a relief station: At least \(1\) of its children must have a relief station.
- If the parent does not have a relief station: At least \(2\) of its children must have relief stations.
That is, in addition to “whether to place a relief station on \(v\),” when not placing one, we also need to distinguish “whether it can receive support from the parent.”
Minimizing Children Independently Is Not Sufficient
When \(v\) does not have a relief station, simply choosing the cheaper option for each child independently might result in fewer children with relief stations than required.
On the other hand, exhaustively searching which children to place relief stations on would take too much time for vertices with many children.
Here, it is sufficient to distinguish only the following \(3\) cases for the number of children with relief stations:
- \(0\)
- Exactly \(1\)
- \(2\) or more
If there are \(2\) or more, the condition is satisfied, so knowing the exact count beyond that is unnecessary. We can process the children one by one using this small DP.
Algorithm
1. Tree DP States
For the subtree rooted at vertex \(v\), we define the following values. In all cases, assume that all vertices in the subtree other than \(v\) satisfy the condition.
| State | Meaning |
|---|---|
selected[v] |
The minimum cost when a relief station is placed on \(v\) |
need_one[v] |
The minimum cost when no relief station is placed on \(v\), and at least \(1\) child has a relief station |
need_two[v] |
The minimum cost when no relief station is placed on \(v\), and at least \(2\) children have relief stations |
need_one[v] is the state used when the parent has a relief station. Combined with the parent’s relief station, there will be at least \(2\) relief stations supporting \(v\).
The cost for unreachable states is set to \(\mathrm{INF}\).
2. When a Relief Station Is Placed on \(v\)
Since there is a relief station at \(v\), there are \(2\) options for each child \(u\):
- Place a relief station on \(u\) as well:
selected[u] - Do not place a relief station on \(u\): since it receives support from its parent \(v\),
need_one[u]
In this case, since there is no restriction on the number of adjacent relief stations for \(v\) itself, we can choose the cheaper option for each child independently.
Letting \(C(v)\) be the set of children of \(v\):
\(\mathrm{selected}[v] = A_v + \sum_{u \in C(v)} \min(\mathrm{selected}[u], \mathrm{need\_one}[u])\)
3. When No Relief Station Is Placed on \(v\)
In this case, child \(u\) cannot receive support from its parent. Therefore, there are \(2\) options:
- Place a relief station on \(u\):
selected[u] - Do not place a relief station on \(u\):
need_two[u]
We process the children one by one, maintaining the following values over the already processed children:
zero: The minimum cost when \(0\) children have relief stationsone: The minimum cost when exactly \(1\) child has a relief stationtwo: The minimum cost when \(2\) or more children have relief stations
Before processing any children:
\(\mathrm{zero}=0,\quad \mathrm{one}=\mathrm{two}=\mathrm{INF}\)
Let \(u\) be the next child to process, and let \(s=\mathrm{selected}[u]\) and \(t=\mathrm{need\_two}[u]\).
When \(0\) children have relief stations
This is only possible if there were previously \(0\), and we do not place one on \(u\) either:
\(\mathrm{next\_zero}=\mathrm{zero}+t\)
When exactly \(1\) child has a relief station
This occurs in one of the following cases:
- There was previously \(1\), and we do not place one on \(u\).
- There were previously \(0\), and we place one on \(u\).
\(\mathrm{next\_one}=\min(\mathrm{one}+t,\ \mathrm{zero}+s)\)
When \(2\) or more children have relief stations
This occurs in one of the following cases:
- There were already \(2\) or more, and we can choose either option for \(u\).
- There was previously \(1\), and we place one on \(u\).
\(\mathrm{next\_two}=\min(\mathrm{two}+\min(t,s),\ \mathrm{one}+s)\)
Assign the updated values to zero, one, and two, then proceed to the next child. In the implementation, each value is clamped so that it does not exceed \(\mathrm{INF}\).
After processing all children:
\(\mathrm{need\_one}[v]=\min(\mathrm{one},\mathrm{two})\)
\(\mathrm{need\_two}[v]=\mathrm{two}\)
4. Computing from Leaves to the Root
To compute the values for each vertex, the values of its children are required. Therefore, children must be processed before their parent.
In the provided code, we first traverse from the root to build an array order where parents appear before their children. By processing this array in reverse order, we compute the tree DP.
For example, for a leaf \(v\) with no children:
selected[v]is \(A_v\)need_one[v]andneed_two[v]are \(\mathrm{INF}\)
This is consistent with the fact that since a leaf has at most \(1\) adjacent vertex, it must always place a relief station on itself.
5. Finding the Answer for the Root
The root has no parent.
Therefore, if we do not place a relief station on the root, at least \(2\) children must have relief stations. The answer is:
\(\min(\mathrm{selected}[0],\mathrm{need\_two}[0])\)
In the code, vertices are \(0\)-indexed, so the root has index \(0\).
Justification of Correctness
When \(v\) has a relief station, each child either “places one on itself” or “receives support from the parent,” and the above transition accounts for both cases.
When \(v\) does not have a relief station, we also consider whether each child “places” or “does not place” one sequentially, categorizing the count into \(0\), \(1\), or \(2\) or more to maintain the minimum cost. Since the required condition is “at least \(1\)” or “at least \(2\)”, this classification is sufficient for correct evaluation.
Therefore, if the children’s DP values are correct, the parent’s DP values will also be computed correctly. By calculating from the leaves up to the root and finally choosing the state that satisfies the conditions for the root, the overall minimum cost is obtained.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(N)\)
Each vertex and edge is processed a constant number of times. The adjacency list, arrays for parents and traversal order, and DP arrays use \(O(N)\) space.
Implementation Notes
Be careful with recursion depth
If the tree is a simple line, the depth can be up to \(N\). As in the provided code, storing the traversal order in an array and processing it in reverse avoids stack overflow caused by recursion.Use 64-bit integers for costs
The total cost when placing relief stations on all vertices can be up to \(2\times 10^{14}\). In C++, uselong long.Represent unreachable states with a sufficiently large value
In the provided code, \(\mathrm{INF}=2^{60}\) is used. This is sufficiently larger than the actual answer, and adding two \(\mathrm{INF}\) values together will not overflowlong long.Use temporary variables when updating for each child
If you updatezero,one, andtwosequentially in place, you may accidentally use the newly updated values. Calculate them into variables likenext_zerofirst, and then assign them all at once.\(N=1\) can be handled with the same logic
Since the root has no children,need_two[0]becomes \(\mathrm{INF}\), and the answer correctly evaluates to \(A_1\).Source Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = (1LL << 60);
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<ll> A(N);
for (ll &x : A) cin >> x;
vector<vector<int>> graph(N);
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
graph[u].push_back(v);
graph[v].push_back(u);
}
vector<int> parent(N, -1), order;
order.reserve(N);
order.push_back(0);
for (int i = 0; i < N; ++i) {
int v = order[i];
for (int u : graph[v]) {
if (u == parent[v]) continue;
parent[u] = v;
order.push_back(u);
}
}
vector<ll> selected(N), need_one(N), need_two(N);
for (int i = N - 1; i >= 0; --i) {
int v = order[i];
ll take = A[v];
ll zero = 0, one = INF, two = INF;
for (int u : graph[v]) {
if (parent[u] != v) continue;
take += min(selected[u], need_one[u]);
ll next_zero = min(INF, zero + need_two[u]);
ll next_one = min({
INF,
one + need_two[u],
zero + selected[u]
});
ll next_two = min({
INF,
two + min(need_two[u], selected[u]),
one + selected[u]
});
zero = next_zero;
one = next_one;
two = next_two;
}
selected[v] = take;
need_one[v] = min(one, two);
need_two[v] = two;
}
cout << min(selected[0], need_two[0]) << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: