E - 二方向から支える救護所 / Relief Stations Supporting from Two Directions 解説 by admin 印刷ページ
Gemini 3.8 Flash (High)Overview
In a tree-structured graph, this problem asks us to minimize the total cost of selecting villages such that for each village, either an aid station is placed in that village, or at least \(2\) directly connected villages have an aid station. Using tree DP (dynamic programming on trees) from the leaves to the root, this can be solved efficiently by maintaining three states: “place an aid station at the village itself”, “do not place at the village itself, but place in at least \(2\) of its children”, and “do not place at the village itself, and place in exactly \(1\) of its children”.
Analysis
Clarifying the Conditions
For each village \(u\), one of the following conditions must hold: 1. Place an aid station at village \(u\) (incurs a cost of \(A_u\)). In this case, there are no additional conditions on its neighboring villages. 2. Do not place an aid station at village \(u\) (cost \(0\)). In this case, at least \(2\) of its neighboring villages must have an aid station.
Designing the Tree DP
Consider the tree as a rooted tree with an arbitrary vertex (village \(1\)) as the root. The neighbors of village \(u\) are divided into its “parent” and its “children”. When no aid station is placed at village \(u\), at least \(2\) neighboring vertices must have aid stations, but the requirements on the children depend on the state of the parent: - If the parent has an aid station: The parent provides \(1\) aid station, so there only needs to be at least \(1\) aid station among its children. - If the parent does not have an aid station (or if \(u\) is the root): The parent cannot be relied upon, so there must be at least \(2\) aid stations among its children.
From this observation, we define the following \(3\) states representing the minimum cost for the subtree \(T_u\):
- dp0[u]: The minimum cost of \(T_u\) when placing an aid station at village \(u\).
- dp1[u]: The minimum cost of \(T_u\) when not placing an aid station at village \(u\), but placing aid stations in at least \(2\) of its children (the condition is satisfied even if the parent has no aid station).
- dp2[u]: The minimum cost of \(T_u\) when not placing an aid station at village \(u\), and placing an aid station in exactly \(1\) of its children (the condition can only be satisfied if the parent has an aid station).
*Note: When the parent has an aid station, having \(2\) or more aid stations among the children is also valid. This case is covered by dp1[u], so taking \(\min(dp1[u], dp2[u])\) from the parent’s perspective naturally represents “at least \(1\) among the children”.
State Transitions
Let \(Ch(u)\) be the set of children of village \(u\), and let \(m\) be the number of children.
- Calculating
dp0[u]: Since an aid station is placed at village \(u\), it incurs a cost of \(A_u\). From the perspective of each child \(v \in Ch(u)\), “the parent has an aid station”, so \(v\) can satisfy its condition in any of the following ways:- Place an aid station at child \(v\) itself (
dp0[v]) - Do not place at child \(v\), and cover it with at least \(2\) of \(v\)’s children (
dp1[v]) - Do not place at child \(v\), and cover it with \(1\) of \(v\)’s children + the parent \(u\) (
dp2[v])
- Place an aid station at child \(v\) itself (
Since we can independently choose the minimum-cost option for each child \(v\), the transition is: $\(dp0[u] = A_u + \sum_{v \in Ch(u)} \min(dp0[v], dp1[v], dp2[v])\)$
- Calculating
dp1[u]anddp2[u]: Since no aid station is placed at village \(u\), from each child \(v\)’s perspective, “the parent does not have an aid station”. Therefore, the choices for child \(v\) are restricted to the following two:- Place an aid station at child \(v\) itself (cost: \(c_0 = dp0[v]\))
- Do not place an aid station at child \(v\) (cost: \(c_1 = dp1[v]\); note that
dp2[v]is not allowed because parent \(u\) has no aid station)
Starting from the state where all children have no aid station (cost \(c_1\)), the cost increase when switching a child to have an aid station (cost \(c_0\)) is \(c_0 - c_1\).
By sorting the children in ascending order of this increase \(c_0 - c_1\), we can greedily determine the optimal combination of children.
- When \(m = 0\) (leaf): No aid station can be placed in children since none exist, so dp1[u] = INF, dp2[u] = INF.
- When \(m = 1\): We cannot choose \(2\) or more children, so dp1[u] = INF. For choosing \(1\) child, dp2[u] = dp0[child].
- When \(m \ge 2\):
Assign the first \(k\) children in sorted order to have aid stations (\(c_0\)), and the remaining \(m - k\) children to not have aid stations (\(c_1\)).
- The value for \(k = 1\) \(\to\) dp2[u]
- The minimum value for \(k \ge 2\) \(\to\) dp1[u]
Answer at the Root (Village \(1\))
Since the root has no parent, dp2[1], which relies on “the parent having an aid station”, is invalid.
Therefore, the answer is \(\min(dp0[1], dp1[1])\).
Algorithm
- Treat the tree as a rooted tree (rooted at village \(1\)), and use BFS or similar to find parent-child relationships and the topological order (reverse order from leaves to root).
- Process each vertex \(u\) in reverse order and fill the DP table:
- Add \(A_u\) to the sum of minimum costs of the children to compute
dp0[u]. - Branch on the number of children \(m\). When \(m \ge 2\), compute the difference \(dp0[v] - dp1[v]\) for each child \(v\) and sort them.
- Using prefix/suffix sums, compute the cost when varying the number of children with aid stations \(k\) over \(1, 2, \dots, m\), and update
dp2[u](\(k=1\)) anddp1[u](minimum over \(k \ge 2\)).
- Add \(A_u\) to the sum of minimum costs of the children to compute
- Finally, output \(\min(dp0[1], dp1[1])\).
Complexity
- Time Complexity: \(O(N \log N)\)
- Letting \(m_u\) be the number of children of vertex \(u\), sorting the children takes \(O(m_u \log m_u)\).
- Since the sum of the number of children across all vertices is \(\sum m_u = N - 1\), the total sorting time over the entire tree is \(\sum O(m_u \log m_u) \le O(N \log N)\).
- BFS and prefix sum calculations take \(O(N)\), so the overall time complexity is \(O(N \log N)\), which is fast enough for the constraint \(N \le 200000\).
- Space Complexity: \(O(N)\)
- \(O(N)\) memory is used for the adjacency list of the graph, the traversal order of vertices, the DP arrays, etc.
Implementation Notes
Corner Case \(N = 1\): When there is only \(1\) village, there are no adjacent villages, so the condition cannot be satisfied without placing an aid station at village \(1\). Thus, the answer is always \(A_1\). Since there are no tree edge inputs (\(N-1\) lines) either, handle this separately at the beginning.
Setting a Sufficiently Large Value (INF): The maximum possible total cost reaches \(N \times \max(A_i) \approx 200000 \times 10^9 = 2 \times 10^{14}\), so
INFshould be set to a value sufficiently larger than this (e.g., \(10^{18}\)), and additions should be checked to prevent overflow.Speedup via Prefix Sums: For the sorted children, by precomputing the prefix sums of \(c_0\) for choosing the first \(k\) children and the suffix sums of \(c_1\) for choosing the remaining \(m - k\) children from the end, the cost for each \(k\) can be evaluated in \(O(1)\), taking \(O(m)\) in total.
Source Code
import sys
def main():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
A = [int(x) for x in input_data[1 : N + 1]]
if N == 1:
print(A[0])
return
adj = [[] for _ in range(N + 1)]
idx = N + 1
for _ in range(N - 1):
u = int(input_data[idx])
v = int(input_data[idx + 1])
idx += 2
adj[u].append(v)
adj[v].append(u)
order = []
children = [[] for _ in range(N + 1)]
visited = [False] * (N + 1)
queue = [1]
visited[1] = True
head = 0
while head < len(queue):
u = queue[head]
head += 1
order.append(u)
for v in adj[u]:
if not visited[v]:
visited[v] = True
children[u].append(v)
queue.append(v)
INF = 10**18
dp0 = [0] * (N + 1)
dp1 = [INF] * (N + 1)
dp2 = [INF] * (N + 1)
for u in reversed(order):
ch = children[u]
m = len(ch)
s = A[u - 1]
for v in ch:
mv = dp0[v]
if dp1[v] < mv:
mv = dp1[v]
if dp2[v] < mv:
mv = dp2[v]
s += mv
dp0[u] = s
if m == 0:
dp1[u] = INF
dp2[u] = INF
elif m == 1:
v = ch[0]
dp1[u] = INF
dp2[u] = dp0[v]
else:
items = []
for v in ch:
c0 = dp0[v]
c1 = dp1[v]
if c1 >= INF:
d = -INF
else:
d = c0 - c1
items.append((d, c0, c1))
items.sort(key=lambda x: x[0])
pref_c0 = [0] * (m + 1)
for i in range(m):
pref_c0[i + 1] = pref_c0[i] + items[i][1]
suff_c1 = [0] * (m + 1)
for i in range(m - 1, -1, -1):
c1 = items[i][2]
if c1 >= INF or suff_c1[i + 1] >= INF:
suff_c1[i] = INF
else:
suff_c1[i] = suff_c1[i + 1] + c1
if suff_c1[1] < INF:
dp2[u] = pref_c0[1] + suff_c1[1]
else:
dp2[u] = INF
min_val = INF
for k in range(2, m + 1):
if suff_c1[k] < INF:
val = pref_c0[k] + suff_c1[k]
if val < min_val:
min_val = val
dp1[u] = min_val
ans = dp0[1]
if dp1[1] < ans:
ans = dp1[1]
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gemini-3.8-flash-high.
投稿日時:
最終更新: