树形dp Codeforces Round #168 (Div. 2) D题 Zero Tree

本文深入解析了ZeroTree算法,一种用于解决树形结构上特定问题的高效算法。该算法通过递归地处理子树,调整节点权值,目标是最小化操作次数使所有节点权值归零。文章详细介绍了算法的实现思路,包括如何通过维护两个数组来记录每个子树的增减操作需求,以及如何在递归过程中更新根节点状态。

Zero Tree

A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices.

A subtree of a tree T is a tree with both vertices and edges as subsets of vertices and edges of T.

You’re given a tree with n vertices. Consider its vertices numbered with integers from 1 to n. Additionally an integer is written on every vertex of this tree. Initially the integer written on the i-th vertex is equal to vi. In one move you can apply the following operation:

Select the subtree of the given tree that includes the vertex with number 1.
Increase (or decrease) by one all the integers which are written on the vertices of that subtree.
Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero.


题目大意:给一颗 n 个结点的树,每个结点都有一个权值,选择一颗包含结点 1 的子树(1是标号不是权值),对这颗子树上的权值减去或者加上 1 ,问最少操作次数可以使树上的每个点权值变为0;

分析可知,对于一颗子树的根结点,最少要加上其子节点的权值的最小值的绝对值(如果子结点权值为负),最少要减去其子节点的权值最大值(如果子结点权值为正);

所以可以开两个数组,decr[i] 表示 i 为根的子树要减去的值,inc[i] 表示 i 为根的子树要加上的值;每次取最大值即可;

注意更新根结点的操作;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100100;
const int M=2000100;
const LL mod=1e9+7;
int head[N],cnt,n; 
LL inc[N],decr[N],a[N];
struct Node{
	int to,nex;
}edge[N*2];
void add(int p,int q){
	edge[cnt].to=q,edge[cnt].nex=head[p],head[p]=cnt++;
}
void dfs(int p,int ft){
	LL mx1=-1,mx2=-1;
	for(int i=head[p];~i;i=edge[i].nex){
		int q=edge[i].to;
		if(q!=ft){
			dfs(q,p);
			mx1=max(mx1,inc[q]),mx2=max(mx2,decr[q]);
		}
	}
	if(mx1==-1&&mx2==-1){
		if(a[p]>=0) decr[p]=a[p];
		else inc[p]=-a[p];
	}
	else{
		decr[p]=mx2,inc[p]=mx1; 
		a[p]+=mx1-mx2;
		if(a[p]>=0) decr[p]+=a[p];
		else inc[p]+=-a[p];
	}
}
int main(){
	memset(head,-1,sizeof(head));
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		int a,b;scanf("%d%d",&a,&b);
		add(a,b),add(b,a);
	}
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	dfs(1,-1);
	printf("%lld\n",decr[1]+inc[1]);
	return 0;
}
对于Codeforces Round 1005 Div. 2中的D解答或解释,当前提供的引用资料并未直接涉及该轮次的比赛目详情。然而,可以基于过往比赛的经验以及相似类型的编程竞赛问提供一般性的指导。 ### 解决方案概述 通常情况下,在解决此类算法竞赛目时,会遵循特定的方法论来处理输入数据并计算所需的结果。虽然具体到Codeforces Round 1005 Div. 2 Problem D的信息未被提及,但可以根据以往经验推测可能涉及到的数据结构和算法技术: - **读取测试案例数量**:程序首先应该能够接收多个独立的测试案例数目\(t\),其中每一个案例都包含了不同的参数集[^3]。 - **解析数组元素**:针对每个测试案例,需解析给定长度为\(n\)的一系列整数\[a_1, a_2,...,a_n\]作为操作对象[^2]。 - **查询次数限制**:需要注意的是,所有测试案例中查询总数不得超过设定的最大值,比如这里提到不超过\(2 \times 10^5\)次查询[^1]。 - **输出格式规定**:当准备打印最终答案时,应按照指定格式输出结果,并继续处理下一个测试案例直到完成全部测试[^4]。 考虑到这些通用原则,如果要设计一个适用于此类型问的解决方案框架,则可能会如下所示: ```python def solve_problem(): import sys input = sys.stdin.read data = input().split() index = 0 results = [] t = int(data[index]) index += 1 for _ in range(t): n = int(data[index]) index += 1 numbers = list(map(int, data[index:index+n])) index += n # 假设这里是解决问的核心逻辑部分, # 需要根据具体的Problem D描述调整这部分代码实现。 result_for_case = "Example Result" results.append(result_for_case) print("\n".join(results)) ``` 上述伪代码展示了如何构建基本架构用于批量处理多组测试用例,但是核心业务逻辑需要依据实际问定义进行填充和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值