E - 二方向から支える救護所 / Relief Stations Supporting from Two Directions 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
We place aid stations at the vertices of a tree with associated costs such that every vertex without an aid station has at least \(2\) adjacent aid stations. Using tree DP that distinguishes whether an aid station is placed at the parent, we find the minimum cost in \(O(N)\).
Analysis
The required number of aid stations among children depends on the parent’s state
Exhaustively searching all subsets of villages to place aid stations would yield \(2^N\) candidates, which is too slow.
Therefore, we root the tree at village \(1\). If we do not place an aid station at vertex \(v\), the number of required adjacent aid stations can be summarized as follows:
- If there is an aid station at the parent: At least \(1\) of the children must have an aid station.
- If there is no aid station at the parent: At least \(2\) of the children must have aid stations.
On the other hand, if we place an aid station at vertex \(v\), there is no condition on adjacent aid stations for \(v\) itself.
Thus, instead of just \(2\) states (“place” / “do not place”), we use a \(3\)-state tree DP where the case of not placing an aid station is further subdivided.
Avoiding exhaustive search over combinations of children
If we do not place an aid station at vertex \(v\), we need to decide which children will have aid stations.
Here, enumerating “which two children to place aid stations at” would result in quadratic time complexity for vertices with many children. For example, in a star tree, computing for the root alone would take \(O(N^2)\).
Instead, we first choose the cheaper state for each child, and then consider the “additional cost to guarantee that an aid station is placed at that child.” Since we only need the \(1\) or \(2\) children with the smallest additional costs, we can find them in a single pass over the children.
Algorithm
1. DP States
For the subtree rooted at vertex \(v\), we define the following. The cost includes only the costs of the aid stations within that subtree.
| State | Meaning |
|---|---|
selected[v] |
Minimum cost when placing an aid station at \(v\) |
need_one[v] |
Minimum cost when not placing at \(v\), but placing at its parent. At least \(1\) child must have an aid station |
need_two[v] |
Minimum cost when not placing at \(v\), and not placing at its parent. At least \(2\) children must have aid stations |
In all cases, all vertices within the subtree must satisfy the conditions. Impossible states are treated as infinity.
2. When placing an aid station at vertex \(v\)
Since there is an aid station at \(v\), for each child \(u\) we can choose either:
- Also place at \(u\):
selected[u] - Do not place at \(u\): since its parent \(v\) has one,
need_one[u]
Since the subtrees of the children are independent, letting \(C(v)\) be the set of children of \(v\):
\(\displaystyle \mathrm{selected}[v]=A_v+\sum_{u\in C(v)}\min(\mathrm{selected}[u],\mathrm{need\_one}[u])\)
3. When not placing an aid station at vertex \(v\)
In this case, the parent of child \(u\) does not have an aid station. Therefore, for each child, we choose either:
- Place at \(u\):
selected[u] - Do not place at \(u\):
need_two[u]
First, ignoring the condition on the number of aid stations placed at the children, we pick the cheaper option for each child. Let this sum be:
\(\displaystyle B=\sum_{u\in C(v)}\min(\mathrm{selected}[u],\mathrm{need\_two}[u])\)
Furthermore, we define the additional cost to guarantee that an aid station is placed at child \(u\) as:
\(\displaystyle d_u=\mathrm{selected}[u]-\min(\mathrm{selected}[u],\mathrm{need\_two}[u])\)
- If placing is cheaper or equal in cost, \(d_u=0\).
- If not placing is cheaper, \(d_u\) is the difference in cost to change to the state where one is placed.
Letting \(d_1\) be the minimum of these additional costs and \(d_2\) be the second smallest value:
\(\mathrm{need\_one}[v]=B+d_1\)
\(\mathrm{need\_two}[v]=B+d_1+d_2\)
If there are not enough children, the corresponding \(d_1, d_2\) are set to infinity.
For example, if the additional costs of the children are \(0,4,7\), guaranteeing placement at at least \(1\) child requires an additional cost of \(0\), and guaranteeing at least \(2\) children requires an additional cost of \(4\).
Since the requirement is “at least”, other children may also have aid stations. For children where placing is already cheaper, we simply leave aid stations placed there.
Why this calculation is optimal
The cost \(B\), obtained by picking the cheaper option for each child, is the minimum cost ignoring the count constraints.
From there, to guarantee placement at \(k\) distinct children, we need the additional costs for those children. Since all additional costs are non-negative, choosing the \(k\) smallest is optimal. Here, it is sufficient to consider only \(k=1,2\).
4. Computing from leaves to the root
Since leaves have no children:
selected[v]is \(A_v\)need_one[v]andneed_two[v]are impossible
Even if there is an aid station at the parent, a leaf can only receive support from \(1\) direction, so an aid station must be placed at the leaf itself.
In the code, we construct an order where parents appear before their children, and process it in reverse. This ensures that when computing for each vertex, the DP values of all its children have already been determined.
5. Finding the answer for the root
The root has no parent.
- If we place an aid station at the root:
selected[0] - If we do not place an aid station at the root, at least \(2\) children need them:
need_two[0]
Therefore, the answer is:
\(\min(\mathrm{selected}[0],\mathrm{need\_two}[0])\)
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
Each vertex and edge is processed a constant number of times. Instead of sorting the additional costs, we only maintain the minimum and second minimum values.
Implementation Notes
Avoid recursion
Since the depth of the tree can be up to \(N\), Python’s recursion limit becomes an issue with recursive DFS. The code constructs anorderand computes the DP in reverse order.Select two distinct children with the minimum additional costs
Maintain the minimum and second minimum values infirstandsecond. Even if they have the same value, both can be used as long as they come from different children.Use a sufficiently large value for impossible states
The cost of placing an aid station at every vertex is at most \(2\times 10^{14}\). Therefore, the code usesINF = 10**18. Even if additions to impossible states exceedINF, it is not an issue because it remains sufficiently larger than any finite optimal value.\(N=1\) can be handled with the same logic
The state where no aid station is placed at the root becomes impossible, and the answer becomes \(A_1\).Source Code
import sys
def main():
read = sys.stdin.buffer.readline
n = int(read())
costs = list(map(int, read().split()))
graph = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = map(int, read().split())
u -= 1
v -= 1
graph[u].append(v)
graph[v].append(u)
parent = [-1] * n
order = [0]
for v in order:
p = parent[v]
for u in graph[v]:
if u != p:
parent[u] = v
order.append(u)
INF = 10**18
selected = [0] * n
need_one = [0] * n
need_two = [0] * n
for v in reversed(order):
chosen_cost = costs[v]
base = 0
first = INF
second = INF
p = parent[v]
for u in graph[v]:
if u == p:
continue
s = selected[u]
a = need_one[u]
b = need_two[u]
chosen_cost += s if s < a else a
if s <= b:
base += s
extra = 0
else:
base += b
extra = s - b
if extra < first:
second = first
first = extra
elif extra < second:
second = extra
selected[v] = chosen_cost
need_one[v] = base + first
need_two[v] = base + first + second
print(min(selected[0], need_two[0]))
if __name__ == "__main__":
main()
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: