【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();
	}
};
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值