E - 三本先を切り離す閉鎖 / Closure Separating Three Lines Ahead 解説 by admin 印刷ページ
Gemini 3.8 Flash (High)Overview
This problem asks for the minimum cost to close (delete) some facilities (vertices) in a tree-structured network so that in each remaining connected component, the distance between any two facilities is at most \(2\) (i.e., the diameter is at most \(2\)).
Noticing that each remaining connected component must be a star graph (star tree), we can solve this problem in \(O(N)\) time using tree DP (tree dynamic programming).
Analysis
1. Understanding the graph structure under the conditions
The condition that one can move between facilities by passing through at most \(2\) corridors means that the diameter (the distance between the two furthest vertices) of each remaining connected component is at most \(2\).
In a tree, a graph with a diameter of at most \(2\) is restricted to one of the following forms: - \(1\) vertex: An isolated vertex (diameter \(0\)) - \(2\) vertices: Two vertices connected by a single edge (diameter \(1\)) - \(3\) or more vertices: A star graph with one “center” vertex to which all other vertices are directly connected (diameter \(2\))
A graph with \(1\) vertex can be viewed as “only a center (with \(0\) leaves)”, and a graph with \(2\) vertices can be viewed as having “one vertex as the center and the other as a leaf”. Therefore, every remaining connected component must be a star graph consisting of “one center and zero or more leaves”.
2. Tree DP Approach
Consider the tree as a rooted tree with an arbitrary vertex (e.g., vertex \(1\)) as the root. Each remaining vertex will become either a “center” or a “leaf”. Considering the parent-child relationships in the rooted tree, the role of each vertex \(u\) can be classified into the following four cases:
- Deleted
- Becomes a center (can connect to children that are leaves)
- Becomes a leaf and connects to its parent (cannot have children, so all its children must be deleted)
- Becomes a leaf and connects to one of its children (does not connect to its parent, and all other children must be deleted)
By performing a tree DP with these as states, we can compute the minimum cost in a bottom-up manner.
Algorithm
DP State Definition
For the subtree rooted at vertex \(u\), we maintain the minimum cost for each of the following four states:
- \(\text{dp}[u][0]\): Vertex \(u\) is deleted
- \(\text{dp}[u][1]\): Vertex \(u\) becomes a center
- \(\text{dp}[u][2]\): Vertex \(u\) is a leaf and connects to its parent
- \(\text{dp}[u][3]\): Vertex \(u\) is a leaf and connects to one of its children
State Transitions
Let \(C(u)\) be the set of all children of \(u\).
\(\text{dp}[u][0]\) (\(u\) is deleted)
- A cost of \(A_u\) is incurred.
- Because \(u\) is deleted, the connections between \(u\) and its children are severed.
- Each child \(v \in C(u)\) can independently be “deleted (\(\text{dp}[v][0]\))”, “become a center (\(\text{dp}[v][1]\))”, or “become a leaf connected to one of its children (\(\text{dp}[v][3]\))” (only \(\text{dp}[v][2]\), where it connects to its parent \(u\), cannot be chosen). $\( \text{dp}[u][0] = A_u + \sum_{v \in C(u)} \min(\text{dp}[v][0], \text{dp}[v][1], \text{dp}[v][3]) \)$
\(\text{dp}[u][1]\) (\(u\) is a center)
- The cost is \(0\).
- Since \(u\) is a center, each child \(v \in C(u)\) must either be “deleted (\(\text{dp}[v][0]\))” or “connect to \(u\) as a leaf (\(\text{dp}[v][2]\))”. $\( \text{dp}[u][1] = \sum_{v \in C(u)} \min(\text{dp}[v][0], \text{dp}[v][2]) \)$
\(\text{dp}[u][2]\) (\(u\) is a leaf and connects to its parent)
- The cost is \(0\).
- Since \(u\) is already connected to its parent, it cannot connect to any children.
- Therefore, all children \(v \in C(u)\) must be deleted. $\( \text{dp}[u][2] = \sum_{v \in C(u)} \text{dp}[v][0] \)$
\(\text{dp}[u][3]\) (\(u\) is a leaf and connects to one of its children)
- The cost is \(0\).
- If \(u\) has no children (\(C(u) = \emptyset\)), it cannot connect to a child, so the value is \(\infty\).
- If \(u\) has children, we choose one child \(c \in C(u)\) to connect to.
- The chosen child \(c\) must be a center (\(\text{dp}[c][1]\)).
- All other children \(v \in C(u) \setminus \{c\}\) must be deleted (\(\text{dp}[v][0]\)). $\( \text{dp}[u][3] = \min_{c \in C(u)} \left( \text{dp}[c][1] + \sum_{v \in C(u), v \neq c} \text{dp}[v][0] \right) \)$
- By precomputing \(S = \sum_{v \in C(u)} \text{dp}[v][0]\), this can be computed in \(O(1)\) for each \(c\) as \(S - \text{dp}[c][0] + \text{dp}[c][1]\).
Final Answer
Since the root has no parent, the state \(\text{dp}[\text{root}][2]\) (where the root connects to its parent) cannot be chosen. Therefore, the answer is the minimum of the following: $\( \min(\text{dp}[\text{root}][0], \text{dp}[\text{root}][1], \text{dp}[\text{root}][3]) \)$
Complexity
- Time Complexity: \(O(N)\)
- Finding the topological order using BFS takes \(O(N)\) time.
- The DP transitions for each vertex take \(O(d)\) time, where \(d\) is the number of children. Across the entire tree, the total time is proportional to the number of edges, which is \(O(N)\).
- Space Complexity: \(O(N)\)
- The adjacency list, DP table, BFS arrays, etc., can all be stored within \(O(N)\) space.
Implementation Details
Bottom-up tree DP without recursion:
- For \(N \le 200000\), deep trees may cause a stack overflow if implemented using recursive calls.
- By using BFS to construct an array
orderof vertices sorted by distance from the root and iterating through it in reverse order (from leaves to the root), we can safely perform the bottom-up DP.
Optimization via difference updates:
- In the computation of \(\text{dp}[u][3]\), by first calculating the total cost \(S\) of deleting all children, and then evaluating the difference for each choice of \(c\) (\(S - \text{dp}[c][0] + \text{dp}[c][1]\)), the transitions can be computed in \(O(|C(u)|)\) overall.
Corner case for \(N = 1\):
When there is only \(1\) facility, the condition is already satisfied without closing any facility, so output \(0\) and terminate.
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 << 0 << "\n";
return 0;
}
// BFSでトポロジカル順(根からの距離順)を取得
vector<int> order;
order.reserve(N);
vector<int> parent(N, -1);
vector<vector<int>> children(N);
vector<bool> visited(N, false);
queue<int> q;
q.push(0);
visited[0] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
order.push_back(u);
for (int v : adj[u]) {
if (!visited[v]) {
visited[v] = true;
parent[v] = u;
children[u].push_back(v);
q.push(v);
}
}
}
// dp[u][0]: 頂点 u を削除
// dp[u][1]: 頂点 u はスターグラフの中心
// dp[u][2]: 頂点 u は葉で、親と接続
// dp[u][3]: 頂点 u は葉で、子の1つと接続
vector<vector<long long>> dp(N, vector<long long>(4, 0));
for (int i = N - 1; i >= 0; --i) {
int u = order[i];
// dp[u][0]: u は削除
dp[u][0] = A[u];
for (int v : children[u]) {
dp[u][0] += min({dp[v][0], dp[v][1], dp[v][3]});
}
// dp[u][1]: u は中心
dp[u][1] = 0;
for (int v : children[u]) {
dp[u][1] += min(dp[v][0], dp[v][2]);
}
// dp[u][2]: u は親と接続する葉
dp[u][2] = 0;
for (int v : children[u]) {
dp[u][2] += dp[v][0];
}
// dp[u][3]: u は子と接続する葉
if (children[u].empty()) {
dp[u][3] = INF;
} else {
long long base = 0;
for (int v : children[u]) {
base += dp[v][0];
}
dp[u][3] = INF;
for (int v : children[u]) {
long long cur = base - dp[v][0] + dp[v][1];
dp[u][3] = min(dp[u][3], cur);
}
}
}
int root = order[0];
long long ans = min({dp[root][0], dp[root][1], dp[root][3]});
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
投稿日時:
最終更新: