CF 274B Zero Tree 树形DP
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.
The first line of the input contains n (1 ≤ n ≤ 105). Each of the next n - 1 lines contains two integers ai and bi(1 ≤ ai, bi ≤ n; ai ≠ bi) indicating there's an edge between vertices ai and bi. It's guaranteed that the input graph is a tree.
The last line of the input contains a list of n space-separated integers v1, v2, ..., vn (|vi| ≤ 109).
Print the minimum number of operations needed to solve the task.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
3
1 2
1 3
1 -1 1
3 题意:
给出一棵树,编号为1~n,点有权值(有小于0的)
你可以做以下操作:
选择树的一棵子树,但是这棵子树必须包含节点1,你可以把这棵子树的所有节点的权值都加1或者都减1
问:至少需要多少个操作,才可以把所有点权都变为0 明显,必须先让深度大的节点点权变为0,最后让根节点点权变为0
一次dfs就可以了
#include<cstdio>
#include<cstring> using namespace std; #define ll long long
const int maxn=1e5+;
inline ll max(ll a,ll b)
{
return a>b?a:b;
} ll add[maxn]; //以i为根的子树需要做加法的次数
ll sub[maxn]; //以i为根的子树需要做减法的次数
//节点i为根的子树,做add[i]次加法和sub[i]次减法后,子树所有节点的点权都为0
ll w[maxn];
struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot=; void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void dfs(int ,int ); int main()
{
memset(head,-,sizeof head);
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
}
for(int i=;i<=n;i++)
{
scanf("%I64d",&w[i]);
}
dfs(,-);
printf("%I64d\n",add[]+sub[]);
return ;
} void dfs(int u,int pre)
{
add[u]=;
sub[u]=;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==pre)
continue;
dfs(v,u);
add[u]=max(add[u],add[v]);
sub[u]=max(sub[u],sub[v]);
}
w[u]=w[u]+add[u]-sub[u];
if(w[u]>=)
sub[u]+=w[u];
else
add[u]+=(-w[u]);
}
CF 274B Zero Tree 树形DP的更多相关文章
- 熟练剖分(tree) 树形DP
熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...
- CF 461B Appleman and Tree 树形DP
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...
- hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...
- CF 486D vailid set 树形DP
As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are gi ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...
- 5.10 省选模拟赛 tree 树形dp 逆元
LINK:tree 整场比赛看起来最不可做 确是最简单的题目. 感觉很难写 不过单独考虑某个点 容易想到树形dp的状态. 设f[x]表示以x为根的子树内有黑边的方案数. 白边方案只有一种所以不用记录. ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- codeforces Round #263(div2) D. Appleman and Tree 树形dp
题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...
随机推荐
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HTML5里autofocus属性
转载:http://www.webhek.com/html5-autofocus/ HTML5给我们带来了一大堆神奇的东西.以前需要用JavaScript和Flash完成的任务,例如表单校验,INPU ...
- feature visualization from ipython notebook
Feature visualization from ipython notebook Wang Xiao 1. install anaconda2 from: https://www.continu ...
- dede模板完全控制攻略
1.使用php代码 @me代表了当前字段的值 {dede:tagname runphp='yes'} @me = "123456";//如果使用了自定义函数 则@me得到的是函数返 ...
- ORA-01089 数据库无法正常关闭
今天在做SOA几个数据库的重启操作,其中一个数据库在关闭过程中一直处于HANG住状态,十几分钟没有任何进展,具体操作过程如下: 一:当时的情景 SQL> shutdown immediate - ...
- C++文件输入和输出
1.引入头文件fstreamfstream头文件定义了用于文件输入的类ifstream和文件输出的类ofstream 2.写文件1)创建一个ofstream对象来管理输出流2)将该对象与文件关联起来3 ...
- mvn使用问题
http://mirrors.ibiblio.org/maven2/org/apache/maven/archetypes/ http://blog.csdn.net/u011340807/artic ...
- C#4.0新特性:可选参数,命名参数,Dynamic
1.可选参数 可以为方法的参数设置一个默认值,如下: class Program { static void Main(string[] args) { Show(); Show("cary ...
- Oracle数据库概述
Oracle是一种RDBMS(Relational Database Management System 关系型数据库管理系统),是Oracle公司的核心产品. 2009年4月,Oracle并购了Su ...
- postfix
http://www.postfix.org/ All programmers are optimists -- Frederick P. Brooks, Jr. 所有程序员都是乐天派