Codeforces 533B Work Group
http://codeforces.com/problemset/problem/533/B
题目大意:
每个人有一个直接的领导,1是总裁,现在要找一个最大的集合,每个领导领导的人的数量都是偶数,问最大的值是多少
思路:
dp:f[i][0]代表以i为根的子树,选出偶数个人的最大值,1反之。
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#define ll long long
ll f[][],v[],ans;
int tot,go[],next[],first[];
int n;
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void dfs(int x){
f[x][]=-0x3f3f3f3f;
f[x][]=;
int pd=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
dfs(pur);pd=;
ll t0=f[x][],t1=f[x][];
f[x][]=std::max(f[pur][]+t0,f[pur][]+t1);
f[x][]=std::max(f[pur][]+t1,f[pur][]+t0);
}
if (!pd){
f[x][]=v[x];
f[x][]=;
return;
}
f[x][]=std::max(f[x][],f[x][]+v[x]);
}
int main(){
n=read();
for (int i=;i<=n;i++){
int x=read(),y=read();
if (x!=-)
insert(x,i);
v[i]=y;
}
dfs();
printf("%I64d\n",f[][]);
return ;
}
Codeforces 533B Work Group的更多相关文章
- 树形dp专栏
前言 自己树形dp太菜了,要重点搞 219D Choosing Capital for Treeland 终于自己做了一道不算那么毒瘤的换根dp 令 \(f[u]\) 表示以 \(u\) 为根,子树内 ...
- Codeforces 626F Group Projects(滚动数组+差分dp)
F. Group Projects time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- codeforces A. Group of Students 解题报告
题目链接:http://codeforces.com/problemset/problem/357/A 题目意思:将一堆人分成两组:beginners 和 intermediate coders .每 ...
- 【CodeForces】626 F. Group Projects 动态规划
[题目]F. Group Projects [题意]给定k和n个数字ai,要求分成若干集合使得每个集合内部极差的总和不超过k的方案数.n<=200,m<=1000,1<=ai< ...
- Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西师大新生赛 L1-2.叶神的字符串
A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 8VC Venture Cup 2016 - Elimination Round F. Group Projects 差分DP*****
F. Group Projects There are n students in a class working on group projects. The students will div ...
- Codeforces Round #207 (Div. 2) A. Group of Students
#include <iostream> #include <vector> using namespace std; int main(){ ; cin >> m ...
- Codeforces 626F Group Projects (DP)
题目链接 8VC Venture Cup 2016 - Elimination Round 题意 把$n$个物品分成若干组,每个组的代价为组内价值的极差,求所有组的代价之和不超过$k$的方案数. ...
- [Codeforces 626F]Group Projects
题目大意: 给定\(n\)个数\(a[1]\sim a[n]\),让你把它分为若干个集合,使每个集合内最大值与最小值的差的总和不超过\(K\).问总方案数. 解题思路: 一道很神的dp题. 首先将数进 ...
随机推荐
- System.Drawing.Image.Save(Savepath),保存为jpg格式,参数错误,文件0kb解决办法
问题场景:asp.net给图片添加文字水印保存为jpg格式时出现标题所描述错误(图片为.jpg格式): 简单验证:用本机的画图程序打开,然后保存为jpg格式会出现警告框"画图程序不能存储该文 ...
- vi查找
/pattern<Enter> :向下查找pattern匹配字符串 ?pattern<Enter>:向上查找pattern匹配字符串 使用了查找命令之后,使用如下两个键快速查找 ...
- spring mvc json 返回乱码问题解决(vestion:3.x.x)
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<spring mvc json 返回乱码问题解决(vestion:3.x.x)> 工程中用springmvc返 ...
- Freemarker生成静态代码实例
1.static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http ...
- MyEclipse 8.0注冊码+原版下载_Java开发软件
MyEclipse是一个十分优秀的用于开发Java, J2EE的Eclipse插件集合,MyEclipse的功能很强大,支持也十分广泛,尤其是对各种开元产品的支持十分不错.MyEclipse眼下支持J ...
- ZigBee心电传输(三)
这段时间因为另外一个项目需要,我搞Zed板去了.现在接着上一步的工作吧,继续把心电做完.这里想要测试一下把心电波形数据传输出来,然后用协调器接收,并从串口显示出来.之后再用ZigBee转蓝牙,从而可以 ...
- Linux系统调用system_call
2016-03-25 张超的<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 我的虚拟环境和代码在http ...
- java基础之导入(药师点评)
/** * 药师点评的导入 * @param request * @param response * @param f * @param tmallTcMessageImport * @return ...
- JavaScript中定时器的暂停和继续
对于JavaScript的定时器来说没有严格意义上的暂停和重启,只有清除停止,但是可以通过一些‘障眼法’实现 allChild[index].onclick = function(){//当点击目标且 ...
- gulp前端自动化构建工具新手入门篇
很久没有更新博文了. 经过了一次年前吐血的赶项目,终于在年后回血了.趁着有空,新学到了一个前端自动化构建工具-gulp. 现在我们通过这3个问题来学习一下: 1.什么是gulp? 2.为什么要用gul ...