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. GJM : Unity3D HIAR -【 快速入门 】 三、导入 SDK

    导入 SDK 本文将向您介绍如何在 Unity 工程中导入 HiAR SDK for Unity.在开始之前,请先访问 HiAR 官网下载最新版本的 SDK. 下载 HiAR SDK for Unit ...

  2. 自己写的HTML5 Canvas + Javascript五子棋

    看到一些曾经只会灌水的网友,在学习了前端之后,已经能写出下载量几千几万的脚本.样式,帮助大众,成为受欢迎的人,感觉满羡慕的.我也想学会前端技术,变得受欢迎呀.于是心血来潮,开始学习前端知识,并写下了这 ...

  3. Android 从零开始打造异步处理框架

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/5995752.html 本文出自[赵彦军的博客] 概述 在Android中会使用异步任务来处理耗时操作,避免出 ...

  4. 【代码笔记】iOS-由身份证号码返回性别

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...

  5. iOS 杂笔-如何解决tableview显示错乱问题

    解决自定义Tableviewcell显示错乱问题 要是要解决这个问题,就需要用到UITableviewCell的prepareForReuse方法 cell被重用如何提前知道? 重写cell的prep ...

  6. 用友NC开发的ListView切换成FormEdit

    在点击 “修改” 按钮或者双击ListView中某一项Item,将进入FormEdit即卡片界面,但是 FormEdit有两种状态,一种是 可编辑的状态(点修改按钮进入的状态),一种是非可编辑状态(双 ...

  7. 0039 Java学习笔记-多线程-线程控制、线程组

    join线程 假如A线程要B线程去完成一项任务,在B线程完成返回之前,不进行下一步执行,那么就可以调用B线程的join()方法 join()方法的重载: join():等待不限时间 join(long ...

  8. Linq语法学习

    关键词: select from where in into join on equals orderby descending DefaultIfEmpty() thenby submitChang ...

  9. linux的七大运行级别及级别修改

    运行级别     级别说明 0           所有进程将被终止,机器将有序的停止,关机时系统处于这个运行级别 1           单用户模式,用于系统维护,只有少数进程运行,同时所有服务也不 ...

  10. WPF 自定义搜索框

      控件中的搜索图标下载地址:http://www.easyicon.net/1183666-Search_icon.html 搜索框设计过程比较简单: 1.先定义一个Rectangle作为背景 2. ...