当前位置: 首页 > news >正文

【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 1in), 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:

  1. All living monsters attack you. Your health decreases by the sum of attack points of all living monsters.
  2. 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 1t104). 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 1n3105).

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} 1ai1012).

The following n − 1 n-1 n1 lines each contain two integers x , y x,y x,y ( 1 ≤ x , y ≤ n 1\le x,y\le n 1x,yn), 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 3105.

输出格式

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 (vsonu&&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=1ilog2nf1,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();}
};

http://www.mrgr.cn/news/45454.html

相关文章:

  • 一款基于uniapp uni picker 封装的一个薪资选择插件 简易版,如有特殊需求,请自行修改源码
  • 串行异步422接口和串行同步422的异同点,分别应用于什么场景
  • Linux 配置MySQL并在C++ 中创建类快速调用
  • 文件传输工具magic-wormhole(比scp好用太多!)
  • springboot实战学习(11)(更新用户基本信息接口主逻辑)
  • 【JavaEE初阶】深入理解不同锁的意义,synchronized的加锁过程理解以及CAS的原子性实现(面试经典题);
  • 常用动词敬语形式大揭秘,柯桥零基础日语培训
  • 在 Vite 中使用 CSS 预处理器的劣势是什么?
  • 【JavaScript】JS核心语法及函数
  • 18747 关键路径
  • ai绘画变现方式全解析,教你如何通过AI绘画赚钱
  • 【Linux】进程间通信——System V消息队列和信号量
  • Nginx跳转模块之location与rewrite
  • 铲屎官有什么推荐的养宠好物吗?如何挑选宠物空气净化器?
  • 清华提出BEV感知和强化学习融合方法:实现感知和决策的无缝衔接
  • 阿里云云虚拟主机SSL证书安装指南
  • C++模拟实现二叉搜索树
  • 总结β光滑性的推论
  • 机器学习概述
  • [Python] 模块导入与字典 : 从入门到进阶