http://codeforces.com/problemset/problem/274/B

题目大意:
 给定你一颗树,每个点上有权值。
 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连通图)且这颗树中必须包含节点1
 然后将这颗子树中的所有点的点权+1或-1
 求把所有点权全部变为0的最小次数(n<=10^5)

题解:

  

因为每一次的子树中都必须有1,所以我们得知每一次变换的话1的权值都会变化

所以我们以1为根

现在,我们发现,如果一个节点的权值发生变化,那么他的父节点的权值一定发生变化

而一个点因为其子节点而导致的,不是用于平衡自己权值的变化是不可避免的

所以我们考虑最小化这个值,我们假设现在他的所有儿子都是叶子节点

那么节点u被变化的值即为inc[u] + dec[u]其中inc[u] = max{inc[v]},dec[u] = max{dec[v]}

inc[u]表示这个节点在最优情况下被加了多少次

dec[u]表示这个节点在最坏情况下被减了多少次

所以我们知道,在我们处理完了u的所有子树的问题后,就要解决自己本身的问题

所以这是根据val[u]的正负来调整inc,和dec

O(n)dfs即可

code

  

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
x=;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=*x+ch-'',ch=getchar(),ch>'!');if(flag) x=-x;
}
template<typename T>inline T cat_max(const T &a,const T &b){return a>b ? a:b;}
template<typename T>inline T cat_min(const T &a,const T &b){return a<b ? a:b;}
const int maxn = ;
struct Node{
int to,next;
}G[maxn<<];
int head[maxn],cnt;
void add(int u,int v){
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
}
ll val[maxn],inc[maxn],dec[maxn];
int fa[maxn];
#define v G[i].to
void dfs(int u){
for(int i = head[u];i;i=G[i].next){
if(v == fa[u]) continue;
fa[v] = u;
dfs(v);
inc[u] = cat_max(inc[u],inc[v]);
dec[u] = cat_max(dec[u],dec[v]);
}
val[u] = val[u] + inc[u] - dec[u];
if(val[u] <= ) inc[u] += -val[u];
else dec[u] += val[u];
return;
}
#undef v
int main(){
int n;read(n);
for(int i=,u,v;i<n;++i){
read(u);read(v);
add(u,v);add(v,u);
}for(int i=;i<=n;++i) read(val[i]);
dfs();
printf("%lld\n",inc[]+dec[]);
getchar();getchar();
return ;
}

CodeForces - 274B Zero Tree的更多相关文章

  1. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  2. Codeforces 765 E. Tree Folding

    题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...

  3. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  4. CodeForces 383C Propagating tree

    Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  5. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  7. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  8. CF 274B Zero Tree 树形DP

    A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following cond ...

  9. Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

随机推荐

  1. intellij中不能导入jar包

    ①选中项目点击右键,选择open modual settings(或者直接按F4): ②在弹出的窗口左端选择Libraries: ③点击左边加号,选择From Maven Repository; ④将 ...

  2. [转载]IIS7报500.23错误的解决方法

    原文出处: 原文作者:pizibaidu 原文链接:http://pizibaidu.blog.51cto.com/1361909/1794446 背景:今天公司终端上有一个功能打开异常,报500错误 ...

  3. JS获取当前时间

    setInterval("getTime();", 1000); function getTime() { //document.getElementById('linkweb') ...

  4. DevOps

    DevOps DevOps(英文Development和Operations的组合)是一组过程.方法与系统的统称,用于促进开发(应用程序/软件工程).技术运营和质量保障(QA)部门之间的沟通.协作与整 ...

  5. Android开发aidl使用中linkToDeath和unlinkToDeath的使用

    1.Binder死亡代理     这一节首先将介绍Binder类中比较重要的两个方法linkToDeath和unlinkToDeath.我们知道Binder是运行在服务进程,若服务端进程因为某种原因“ ...

  6. 2016总结Android面试题

    1.简单的设计模式:单例模式:在系统中一个类只有一个实例. 分为懒汉模式和饿汉模式.饿汉模式的代码如下:public class Singleten{private static singleten ...

  7. 事件分发时候的onTouchEvent,onInterceptTouchEvent,dispatchTouchEvent调用顺序

    一直想弄清楚onTouchEvent,onInterceptTouchEvent,dispatchTouchEvent的执行顺序,以及内部使用switch (event.getAction())中的执 ...

  8. 多站点配置apache服务器

    以阿里云服务器为例,使用的是阿里云web一键安装包 目录: /alidata/server/httpd-2.4.10/conf/extra 代码内容: <VirtualHost *:80> ...

  9. ddd 聚合根 之 聚合与不聚合 设计

    聚合 不聚合 订单和订单明细 论坛主贴与贴子回复 订单和收货地址(vo)  

  10. 怎么样在Myeclipse中配置JDK?

    1.首先电脑上安装JDK 2.打开Myeclipse  >>  Window  >>  Preferences  如图1: 图1 2.Preferences  >> ...