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. iOS角度与弧度转换

    在iOS中图片的旋转单位为弧度而不是角度,所以经常会在两者之间进行转换 弧度转角度 #define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / ...

  2. android ImageLoader 混淆加载drawable出现黑色图片的解决方案

    在网上找了很久,没有找到.后来看了源码才知道... 多线程异步加载和显示图片(图片来源于网络.sd卡.assets文件夹,drawable文件夹(不能加载9patch),新增加载视频缩略图) Stri ...

  3. 使用Fiddler抓取手机请求

    使用Fiddler抓取手机请求 Fiddler 手机 今天想尝试在手机上抓包,发现一个好玩的小工具——Fiddler. Fiddler是一个专门的抓包工具,可以模拟请求,修改请求,手机应用调试等.还是 ...

  4. [数据分析]excel带名称的四象限散点图制作

    本文前言:方法来至Excel图表之道这本数,偶然看到,好久没出数据分析的文章了,也难怪最近几个月都忙作网页,数据分析自己也就用excell和sql 正文: 带象限的散点图效果如下: 看到图片,这里制作 ...

  5. 《Note --- Unreal 4 --- behavior tree》

    Web: https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/index.html Test project: D:\En ...

  6. [AC自动机]【学习笔记】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...

  7. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

  8. polya/burnside 学习

    参考链接: http://www.cnblogs.com/hankers/archive/2012/08/03/2622231.html http://blog.csdn.net/raalghul/a ...

  9. [python]设计模式

    需要说明:java跟python在思维模式上并不一样,java利用接口以及多态可以实现很多抽象上的东西,而python不行,其实以下很多设计模式写法并不适用也没有必要,更多是为了对比和帮助理解这些设计 ...

  10. Centos 7 minimal install 无网络无ifconfig的解决

    Centos7这个比较不厚道, minimal install下居然不带net-tools 先要连上网络 修改/etc/sysconfig/network-scripts/ifcfg-ens12312 ...