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的更多相关文章

  1. 树形dp专栏

    前言 自己树形dp太菜了,要重点搞 219D Choosing Capital for Treeland 终于自己做了一道不算那么毒瘤的换根dp 令 \(f[u]\) 表示以 \(u\) 为根,子树内 ...

  2. Codeforces 626F Group Projects(滚动数组+差分dp)

    F. Group Projects time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  3. codeforces A. Group of Students 解题报告

    题目链接:http://codeforces.com/problemset/problem/357/A 题目意思:将一堆人分成两组:beginners 和 intermediate coders .每 ...

  4. 【CodeForces】626 F. Group Projects 动态规划

    [题目]F. Group Projects [题意]给定k和n个数字ai,要求分成若干集合使得每个集合内部极差的总和不超过k的方案数.n<=200,m<=1000,1<=ai< ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #207 (Div. 2) A. Group of Students

    #include <iostream> #include <vector> using namespace std; int main(){ ; cin >> m ...

  8. Codeforces 626F Group Projects (DP)

    题目链接  8VC Venture Cup 2016 - Elimination Round 题意  把$n$个物品分成若干组,每个组的代价为组内价值的极差,求所有组的代价之和不超过$k$的方案数. ...

  9. [Codeforces 626F]Group Projects

    题目大意: 给定\(n\)个数\(a[1]\sim a[n]\),让你把它分为若干个集合,使每个集合内最大值与最小值的差的总和不超过\(K\).问总方案数. 解题思路: 一道很神的dp题. 首先将数进 ...

随机推荐

  1. 【HDOJ】1104 Remainder

    bfs. #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> ...

  2. 单网卡多IP导致的socket connect 10060超时错误

    问题: 接管别人代码时遗留的一个bug,在win7下,给一个网卡设置多个ip时,发现无法连接上服务器了.XP下就不会,这多个ip为192.168.1.127,172.1.1.13,10.0.0.1. ...

  3. velocity序列动画

          结合上次提到的velocity的UI Pack存在一下问题: 动画名称过长,语意性差 使用UI Pack的动画,loop属性会失效 无法监听动画完成时机        我这里想到了一种解决 ...

  4. Gulp-livereload:实时刷新编码

    实现功能 监听指定目录下的所有文件,实时动态刷新页面 安装(Install) 功能的实现是借助 gulp-connect 插件完成的;所以,首先通过下面命令完成插件安装: npm install -- ...

  5. 【C#基础】CSA控件编写秘籍

    新建CSA控件 1.新建一个纯类,命名空间是: namespace SimPerfect.CSAControlLibrary.CSAControls 2.实现两个构造函数:无参和传Candy参数 pu ...

  6. Android-自定义PopupWindow

    PopupWindow在应用中应该是随处可见的,很常用到,比如在旧版本的微信当中就用到下拉的PopupWindow,那是自定义的.新版微信5.2的ActionBar,有人已经模仿了它,但微信具体是使用 ...

  7. Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1) 错误 解决方案(android-ndk)

    在android里做ndk编程的时候,碰到个随机性错误 错误信息如下: 05-06 15:59:44.411: A/libc(3347): Fatal signal 11 (SIGSEGV) at 0 ...

  8. Redis源代码分析-内存数据结构intset

    这次研究了一下intset.研究的过程中,一度看不下过去,可是还是咬牙挺过来了.看懂了也就是那么回事.静下心来,切莫浮躁 Redis为了追求高效,在存储下做了非常多的优化,像intset就是作者为了节 ...

  9. [Angular 2] *ngFor with index

    Let's see how to track index when we use 'ngFor: <li *ngFor="#hero of heros | async, #i = in ...

  10. Java基础知识强化42:StringBuffer类之StringBuffer的截取功能

    1. StringBuffer的截取功能: public String subString(int Start): public String subString(int Start, int end ...