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题. 首先将数进 ...
随机推荐
- 【转】锋狂百科:手机也能接外设 OTG技术详解
原文网址:http://www.gfan.com/review/2014030346245.html 说到USB数据接口,相信大家并不陌生,在日常使用各类数码产品时我们几乎都会用到它.例如最常用的U盘 ...
- Java 8 默认方法和多继承深入解析
以前经常谈论的Java对比c++的一个优势是Java中没有多继承的问题. 因为Java中子类只能继承(extends)单个父类, 尽管可以实现(implements)多个接口,但是接口中只有抽象方法, ...
- 解决 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
"Accept-Encoding": "gzip, deflate", 这条信息代表本地可以接收压缩格式的数据,而服务器在处理时就将大文件压缩再发回客户端,IE ...
- CSS(三)
CSS又上完了,真是快!!! 预习了JS的一部分,写了几条简单的JS代码: 1.成绩判定: <!DOCTYPE html> <html lang="en"> ...
- SUBTOTAL 函数与所在行
SUBTOTAL 函数与所在行 设计要点:数据汇总.隐藏.筛选 阿金:给你推荐一个函数SUBTOTAL. 秀秀:又是函数!俺不喜欢. 阿金:虽然你不喜欢函数,但是你也离不了啊,比如汇总. 秀秀:那倒是 ...
- 【MongoDB数据库】怎样安装、配置MongoDB
本blog以最简洁的方式记录了博主在折腾MongoDB过程中点点滴滴,当中包含下载MongoDB.配置环境变量.怎样启动MongoDBserver.怎样连接MongoDBserver以及怎样连接Mon ...
- 在VM中安装Android4.4连接小米手环 之 在VM中安装Android4.4
今天刚买了个小米手环,系统须要4.4及以上,但自己手机系统版本号不匹配.故打算在VM中安装Android4.4连接小米手环. 这一节先介绍在VM中安装Android4.4(怎么安装VM就不介绍了) 1 ...
- api接口、RPC、WebService REST
RPC:所谓的远程过程调用 (面向方法) SOA:所谓的面向服务的架构(面向消息) REST:所谓的 Representational state transfer (面向资源) RPC 即远程过程调 ...
- thinkphp学习资料
http://www.5idev.com/p-thinkphp_auto_function_callback.shtml
- linux命令之seq
seq命令简述 seq命令比较常用,在需要做循环的时候用于产生一个序列是再合适不过的工具了,常用方法也比较简单: Usage: seq [OPTION]... LAST seq [ ...