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

题目大意

You are given a rooted tree, consisting of nnn vertices. The vertices in the tree are numbered from 111 to nnn, and the root is the vertex 111. The value aia_iai is written at the iii-th vertex.

You can perform the following operation any number of times (possibly zero): choose a vertex vvv which has at least one child; increase ava_vav by 111; and decrease aua_uau by 111 for all vertices uuu that are in the subtree of vvv (except vvv 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.
给你一棵有根的树,由 nnn 个顶点组成。树中的顶点编号为 111nnn ,根顶点为 111 。值 aia_iai 写在第 iii 个顶点上。

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

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

输入数据

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

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

The second line contains nnn integers a1,a2,…,ana_1, a_2, \dots, a_na1,a2,,an (0≤ai≤1090 \le a_i \le 10^90ai109) — the initial values written at vertices.

The third line contains n−1n-1n1 integers p2,p3,…,pnp_2, p_3, \dots, p_np2,p3,,pn (1≤pi≤n1 \le p_i \le n1pin), where pip_ipi is the parent of the iii-th vertex in the tree. Vertex 111 is the root.

Additional constraint on the input: the sum of nnn over all test cases doesn’t exceed 2⋅1052 \cdot 10^52105.

输出数据

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

我们思考

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

我们再思考

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

我们再再思考

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

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

#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 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值