【动态规划】之树形DP例题2024/7/10codeforces Div.2 T4《Maximize the Root》

题目大意

You are given a rooted tree, consisting of n n n vertices. The vertices in the tree are numbered from 1 1 1 to n n n, and the root is the vertex 1 1 1. The value a i a_i ai is written at the i i i-th vertex.

You can perform the following operation any number of times (possibly zero): choose a vertex v v v which has at least one child; increase a v a_v av by 1 1 1; and decrease a u a_u au by 1 1 1 for all vertices u u u that are in the subtree of v v v (except v v v itself). However, after each operation, the values on all vertices should be non-negative.

Your task is to calculate the maximum possible value written at the root using the aforementioned operation.
给你一棵有根的树,由 n n n 个顶点组成。树中的顶点编号为 1 1 1 n n n ,根顶点为 1 1 1 。值 a i a_i ai 写在第 i i i 个顶点上。

您可以执行以下任意次数(可能为零)的操作:选择一个顶点 v v v 它至少有一个子顶点;将 a v a_v av 增加 1 1 1 ;并将 a u a_u au 减少 1 1 1 ,所有顶点 u u u 都在 v v v 的子树中( v v v 本身除外)。不过,每次操作后,所有顶点上的值都应为非负。

您的任务是通过上述操作计算出写入根的最大可能值。

输入数据

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105) — the number of vertices in the tree.

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 0 ≤ a i ≤ 1 0 9 0 \le a_i \le 10^9 0ai109) — the initial values written at vertices.

The third line contains n − 1 n-1 n1 integers p 2 , p 3 , … , p n p_2, p_3, \dots, p_n p2,p3,,pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin), where p i p_i pi is the parent of the i i i-th vertex in the tree. Vertex 1 1 1 is the root.

Additional constraint on the input: the sum of n n n over all test cases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输出数据

For each test case, print a single integer — the maximum possible value written at the root using the aforementioned operation.

样例

输入

3
4
0 1 0 2
1 1 3
2
3 0
1
5
2 5 3 9 6
3 1 5 2

输出

1
3
6

我们思考

看到 N N N 个节点和 N − 1 N-1 N1 条边,我们很容易想到这是一个树状结构,就是说给这个节点加一后,它的所有叶子节点都要减一。
那么最重要的一定是子节点中最小的那个,那么我们用 f f f 数组记录如果让这个点一直加最多加能多少,这个值为他下面所有点的最小值。但是这样的话把所有从下往上数第二层的数一直加不就好了?怎么可能这么简单呢,所以一定是错的。

我们再思考

显然对于每个节点,当前最优值并不一定为最终的最优值,所以可以想到树形DP,将所有可能更优的结果补充不漏地考虑到,得到最终的最优结果。

我们再再思考

对于第 i i i 个点,假如它只有一个连着的子节点,这个子节点可能有若干子节点,我这里说的假如只有一个连着的子节点指的是亲生的,而这个唯一的亲生子节点其实指的是最小的那个子节点,会影响到当前节点所能执行操作次数的节点,那么我们如何将这个点贡献最大化呢?
因为这个点同时受到自己的子节点的影响,这个点或其子节点过小都会导致点 i i i 所能执行的操作次数过小,所以我们可以让这个点等于它与最小子节点的平均数。因为这个点加了子节点就都减少嘛,所以直接取个平均值。

也就是说第 x x x 个点等于它与所有子节点中最小的那个和点 x x x 的平均数然后向上传递。
然后我们将这个状态传递到第二层(假设最顶端节点 1 1 1 为第一层),那么是不是就可以构建出一个最小值最大的树了呢,然后让根节点原本的值加上这个最小值,就做出来了。

#include<bits/stdc++.h>
using namespace std;
#define N 200010
#define M 1000000010
int f[N] , a[N] , ver[N] , hd[N] , nxt[N] , p , ans , tot , T , n;
void lian(int x , int y)
{
	ver[++ tot] = y ;
	nxt[tot] = hd[x] ;
	hd[x] = tot ;
}
void dfs(int x)
{
	for(int i = hd[x] ; i ; i = nxt[i])
	{
		int y = ver[i] ;
		dfs(y);
		f[x] = min(f[x] , f[y]);
	}
	if(f[x] != M) f[x] = min(f[x] , (f[x] + a[x]) / 2);
    else f[x] = a[x];
    return ;
}
int main()
{
	cin >> T;
	while( T --)
	{
		cin >> n; 
		ans = M ; tot = 0 ;
		
		for(int i = 1 ; i <= n ; i ++)
		{
			cin >> a[i];
			hd[i] = 0; f[i] = M ;
		}
		for(int i = 2 ; i <= n ; i ++)
		{
			cin >> p;
			lian(p , i);
		}
		dfs(1);
		for(int i = hd[1] ; i ; i = nxt[i])
			ans = min(ans , f[ver[i]]);
		cout << ans + a[1] <<endl;
	}
	return 0 ;
}
  • 15
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值