E - 二方向から支える救護所 / Relief Stations Supporting from Two Directions Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
In this problem, on a tree-structured graph, we need to place relief stations such that each village either “has a relief station” or “if it does not have one, at least \(2\) of its adjacent villages have a relief station”. Noticing that the required number of relief stations among the children depends on whether the parent has a relief station, this problem can be solved using Tree DP (Dynamic Programming on Trees).
Analysis
Tree Structure and Relief Station Conditions
Since the given graph is a tree, we can treat it as a rooted tree by choosing an arbitrary vertex (e.g., vertex \(1\)) as the root. For each vertex \(u\), its adjacent vertices are categorized into its “parent” and its “children”.
Summarizing the conditions for vertex \(u\), we can divide them into the following two cases: 1. When placing a relief station at \(u\): - A cost of \(A_u\) is incurred. - The condition for \(u\) itself is satisfied. In addition, it provides one adjacent relief station to its adjacent vertices (its parent and children). 2. When NOT placing a relief station at \(u\): - At least \(2\) of the vertices adjacent to \(u\) must have relief stations. - The number of relief stations required among its children depends on whether the parent has a relief station: - When the parent has a relief station: At least \(1\) relief station is required among the children. - When the parent does not have a relief station: At least \(2\) relief stations are required among the children.
Note: Since a leaf (a vertex with \(0\) children) only has its parent as an adjacent vertex, even if the parent has a relief station, it has at most \(1\) adjacent relief station, which cannot satisfy the condition (for \(N \ge 2\)). Therefore, a relief station must always be placed at every leaf.
DP State Definition
To apply Tree DP, we define the states representing the minimum cost for the subtree rooted at \(u\) as follows:
dp[u][0]: The minimum cost when placing a relief station at \(u\).dp[u][1]: The minimum cost when not placing a relief station at \(u\) and not placing one at the parent of \(u\).- In this case, at least \(2\) of the children of \(u\) must have relief stations.
dp[u][2]: The minimum cost when not placing a relief station at \(u\) and placing one at the parent of \(u\).- In this case, at least \(1\) of the children of \(u\) must have a relief station.
State Transitions
Let \(C(u)\) be the set of children of vertex \(u\).
Computing
dp[u][0]:- We pay the cost \(A_u\).
- Since \(u\) has a relief station, from the perspective of each child \(v \in C(u)\), the parent has a relief station.
- Each child \(v\) can choose whichever is cheaper between “placing a relief station at itself (
dp[v][0])” and “not placing one at itself (dp[v][2])”. $\(dp[u][0] = A_u + \sum_{v \in C(u)} \min(dp[v][0], dp[v][2])\)$
Computing
dp[u][1]anddp[u][2]:- Since we do not place a relief station at \(u\), from the perspective of each child \(v \in C(u)\), the parent does not have a relief station.
- Therefore, the choices for each child \(v\) are
dp[v][0](placing one) ordp[v][1](not placing one). - Here, at least \(K\) vertices in \(C(u)\) must have relief stations (i.e., choose
dp[v][0]), where \(K = 2\) fordp[u][1]and \(K = 1\) fordp[u][2]. - If the total number of children satisfies \(|C(u)| < K\), the condition cannot be satisfied, so the cost is \(\infty\).
- The minimum cost of choosing at least \(K\) relief stations from the children can be determined greedily as follows:
- First, for each child \(v\), choose \(\min(dp[v][0], dp[v][1])\) and sum up the costs.
- Count the number of children where a relief station was placed at this stage (i.e., where \(dp[v][0] \le dp[v][1]\)).
- If the number of placed relief stations is less than \(K\), select from the children where a station was not placed in ascending order of the cost difference when placing one (\(dp[v][0] - dp[v][1]\)), and add the differences until reaching \(K\) stations.
Algorithm
- Perform a BFS or DFS rooted at an arbitrary vertex (e.g., vertex \(0\)) to record the parent-child relationships and the visiting order (from parent to children).
- In reverse visiting order (from leaves to the root), compute the Tree DP for each vertex \(u\):
- Compute
dp[u][0]. - Compute the cost of selecting children with \(K = 2\), and assign it to
dp[u][1]. - Compute the cost of selecting children with \(K = 1\), and assign it to
dp[u][2].
- Compute
- Since the root has no parent, the overall answer is the minimum of
dp[root][0](placing a relief station at the root) anddp[root][1](not placing one at the root). - Note that if \(N = 1\), there are no adjacent vertices, so we have no choice but to place a relief station, making the answer \(A_1\).
Complexity
- Time Complexity: \(O(N \log N)\)
- Letting \(d_u\) be the degree (number of children) of vertex \(u\), sorting the differences at each vertex takes \(O(d_u \log d_u)\).
- Summed over all vertices, this gives \(\sum O(d_u \log d_u) \le O(N \log N)\), which is well within the time limit. (Since \(K \le 2\), finding the \(K\) smallest values in linear time can also achieve \(O(N)\)).
- Space Complexity: \(O(N)\)
- The adjacency list of the tree, DP table, BFS arrays, etc., all consume memory proportional to the number of vertices and edges, resulting in \(O(N)\).
Implementation Notes
Corner case for \(N = 1\): Since parts of the Tree DP transitions assume the existence of parents or children, it is safer to handle \(N = 1\) separately at the beginning by outputting \(A_1\) and terminating.
Caution regarding overflow: Because the total cost can exceed the range of a \(32\)-bit signed integer, use a \(64\)-bit integer type (such as
long longin C++) for the DP table and the infinity value (INF).Source Code
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const long long INF = 1e17;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
if (!(cin >> N)) return 0;
vector<long long> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
vector<vector<int>> adj(N);
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
--u; --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (N == 1) {
cout << A[0] << "\n";
return 0;
}
// BFSで根付き木を構築
vector<int> parent(N, -1);
vector<vector<int>> children(N);
vector<int> order;
order.reserve(N);
vector<bool> vis(N, false);
queue<int> q;
q.push(0);
vis[0] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
order.push_back(u);
for (int v : adj[u]) {
if (!vis[v]) {
vis[v] = true;
parent[v] = u;
children[u].push_back(v);
q.push(v);
}
}
}
// dp[u][0]: u に救護所を置く
// dp[u][1]: u に救護所を置かず、u の親にも置かない(子の救護所数 >= 2)
// dp[u][2]: u に救護所を置かず、u の親に置く(子の救護所数 >= 1)
vector<vector<long long>> dp(N, vector<long long>(3, 0));
auto calc = [&](int u, int K) -> long long {
int m = children[u].size();
if (m < K) return INF;
long long S = 0;
int cnt = 0;
vector<long long> diffs;
for (int v : children[u]) {
long long c1 = dp[v][0];
long long c0 = dp[v][1];
if (c1 <= c0) {
S += c1;
cnt++;
} else {
S += c0;
diffs.push_back(c1 - c0);
}
}
if (cnt >= K) return S;
int need = K - cnt;
sort(diffs.begin(), diffs.end());
for (int j = 0; j < need; ++j) {
S += diffs[j];
}
return S;
};
for (int i = N - 1; i >= 0; --i) {
int u = order[i];
// dp[u][0]
long long cost0 = A[u];
for (int v : children[u]) {
cost0 += min(dp[v][0], dp[v][2]);
}
dp[u][0] = cost0;
// dp[u][1]
dp[u][1] = calc(u, 2);
// dp[u][2]
dp[u][2] = calc(u, 1);
}
long long ans = min(dp[0][0], dp[0][1]);
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: