【Codeforces】CF 1998 D
The Omnipotent Monster Killer
#树形dp #枚举
题目描述
You, the monster killer, want to kill a group of monsters. The monsters are on a tree with n n n vertices. On vertex with number i i i ( 1 ≤ i ≤ n 1\le i\le n 1≤i≤n), there is a monster with a i a_i ai attack points. You want to battle with monsters for 1 0 100 10^{100} 10100 rounds.
In each round, the following happens in order:
- All living monsters attack you. Your health decreases by the sum of attack points of all living monsters.
- You select some (possibly all or none) monsters and kill them. After being killed, the monster will not be able to do any attacks in the future.
There is a restriction: in one round, you cannot kill two monsters that are directly connected by an edge.
If you choose what monsters to attack optimally, what is the smallest health decrement you can have after all rounds?
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104). Description of the test cases follows.
The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 3 ⋅ 1 0 5 1\le n\le 3\cdot 10^5 1≤n≤3⋅105).
The second line of each test case contains n n n integers a 1 , … , a n a_1,\ldots,a_n a1,…,an ( 1 ≤ a i ≤ 1 0 12 1\le a_i\le 10^{12} 1≤ai≤1012).
The following n − 1 n-1 n−1 lines each contain two integers x , y x,y x,y ( 1 ≤ x , y ≤ n 1\le x,y\le n 1≤x,y≤n), denoting an edge on the tree connecting vertex x x x and y y y.
It is guaranteed that the sum of n n n over all test cases does not exceed 3 ⋅ 1 0 5 3\cdot 10^5 3⋅105.
输出格式
For each test case, print one integer: the minimum possible health decrement.
样例 #1
样例输入 #1
3
1
1000000000000
5
47 15 32 29 23
1 2
1 3
2 4
2 5
7
8 10 2 3 5 7 4
1 2
1 4
3 2
5 3
6 2
7 5
样例输出 #1
1000000000000
193
57
解题思路
首先对于点的删除,总共的次数不会很多,因为最差情况下也能选择树种一半的节点,然后删除大的那一部分,因此总共次数在 ⌈ l o g 2 n ⌉ \lceil log_2n \rceil ⌈log2n⌉次。
由于每个节点之间具有依赖关系,难以贪心,因此容易想到使用树形 d p dp dp来具体选择什么时候删除哪些节点。
因此,我们可以设计状态 f u , i f_{u,i} fu,i表示 u u u节点在第 i i i轮删除时,其子树的最小代价,有如下转移方程:
f u , i = f u , i + f v , j ( v ∈ s o n u & & j ≠ i ) f_{u,i} = f_{u,i}+f_{v,j} \ (v \in son_u \&\&j \ \neq i) fu,i=fu,i+fv,j (v∈sonu&&j =i)
因为轮次只有 l o g 2 n log_2n log2n种,因此我们可以暴力枚举所有的状态,从子树向根转移,最后的答案就是 min i = 1 i ≤ l o g 2 n f 1 , i \min_{i=1}^{i\leq log_2n} f_{1,i} mini=1i≤log2nf1,i
代码
void solve() {int n;std::cin >> n;std::vector<int>a(n + 1);for (int i = 1; i <= n; ++i) {std::cin >> a[i];}std::vector<std::vector<int>> e(n + 1);for (int i = 1; i <= n - 1; ++i) {int u, v;std::cin >> u >> v;e[u].emplace_back(v);e[v].emplace_back(u);}std::vector<std::array<int,24>>f(n + 1);auto dfs = [&](auto&& self, int u, int fa)->void {for (int i = 1; i <= 23; ++i) {f[u][i] = i * a[u];}for (auto& v : e[u]) {if (v == fa) continue;self(self, v, u);for (int i = 1; i <= 23; ++i) {int minx = inf;for (int j = 1; j <= 23; ++j) {if (i == j) continue;minx = std::min(minx, f[v][j]);}f[u][i] += minx;}}};dfs(dfs, 1, -1);int res = *std::min_element(f[1].begin() + 1, f[1].end());std::cout << res << "\n";
}signed main() {std::ios::sync_with_stdio(0);std::cin.tie(0);std::cout.tie(0);int t = 1;std::cin >> t;while (t--) {solve();}
};