樹形dp Codeforces Round #168 (Div. 2) D題 Zero Tree

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章