CodeForces - 274B Zero Tree
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的更多相关文章
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- Codeforces 765 E. Tree Folding
题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- codeforces 375D:Tree and Queries
Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...
- 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 ...
- 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 ...
随机推荐
- Drupal 8.2.4安装简体中文步骤
安装的时候发现很多情况下会出现各种问题,现在写下自己安装成功的步骤: 1.首先官网下载zip安装包drupal-8.2.4.zip 2.下载官方提供的8.2.4简体中文语言包drupal-8.2.4. ...
- script标签中defer和async属性的区别
这篇文章来源于JS高级程序设计第三版中关于script标签的介绍,结合查阅的资料写下的学习笔记. 向html页面中插入javascript代码的主要方法就是通过script标签.其中包括两种形式,第一 ...
- HTML的doctype和编码
DOCTYPE Doctype是为了告诉浏览器用哪种版本的html去解析指令,必须位于第一行,在某些版本中需要引入DTD,html5无需引入DTD 中文乱码 原因 浏览器解析代码的编码格式与源代码编码 ...
- SAP GUI的配置文件
GUI是SAP系统最常用的客户端,在一台客户机上,利用GUI可以连接多套SAP系统(连接方法参见<客户端连接配置(SAP GUI 710)>),也可以设置多个快捷方式登录(参见<用快 ...
- ArcGIS Engine开发前基础知识(3)
对象模型图 一.对象模型图中的类与接口 ArcGIS Engine 提供大量的对象,这些对象之间存在各种各样的关系,如继承.组合.关联等.对象模型图(Object model diagram,ODM) ...
- Javascript前端和JAVA后端对加密库的处理实例
前端加密 Javascript的加解密有开源的库,http://www.oschina.net/p/crypto-js/ 如下是具体的使用例子 <!DOCTYPE html> <ht ...
- CALayer的transform属性
先来与View比较一下 View:transform -> CGAffineTransformRotate... layer:transform -> CATransform3DRotat ...
- docker通过iptables修改或新增镜像映射端口
443 8088 22 端口是初始映射端口 [root@SERVER ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAM ...
- MySQL:procedure, function, cursor,handler
Procedure & Function Procedure 语法: CREATE [DEFINER = { user | CURRENT_USER }] PROCEDURE sp_name ...
- hadoop-2.7.1伪分布环境搭建
1.准备Linux环境 1.0 点击VMware快捷方式,右键打开文件所在位置 -> 双击vmnetcfg.exe -> VMnet1 host-only ->修改subnet i ...