BZOJ1722 [Usaco2006 Mar] Milk Team Select 产奶比赛
直接树形dp就好了恩
令$f[i][j][t]$表示以$i$为根的子树,选出来的点存在$j$对父子关系,$t$表示$i$这个点选或者没选,的最大产奶值
分类讨论自己和儿子分别有没有选,然后转移一下就好了。。。恩,详情看代码好了
/**************************************************************
Problem: 1722
User: rausen
Language: C++
Result: Accepted
Time:28 ms
Memory:2808 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std;
const int N = ;
const int inf = 1e9; struct edge {
int next, to;
edge(int _n = , int _t = ): next(_n), to(_t) {}
} e[N]; struct tree_node {
int v, fa;
} tr[N]; int n, tar, ans;
int first[N], tot;
int f[N][N][]; inline void add_edge(int x, int y) {
e[++tot] = edge(first[x], y);
first[x] = tot;
} #define y e[x].to
void dfs(int p) {
int t1, t2, tmp, i, j, x;
static int t[N];
f[p][][] = , f[p][][] = tr[p].v;
for (i = ; i < n; ++i)
f[p][i][] = f[p][i][] = -inf;
if (first[p] == ) return;
for (x = first[p]; x; x = e[x].next) {
dfs(y);
for (t1 = ; t1 < ; ++t1) {
for (j = ; j < n; ++j) t[j] = f[p][j][t1];
for (t2 = ; t2 < ; ++t2) {
tmp = t1 && t2 && p;
for (i = ; i < n; ++i) if (f[y][i][t2] != -inf)
for(j = n - ; i + tmp <= j; --j)
if (f[p][j - i - tmp][t1] != -inf)
t[j] = max(t[j], f[p][j - i - tmp][t1] + f[y][i][t2]);
}
for (j = ; j < n; ++j) f[p][j][t1] = max(f[p][j][t1], t[j]);
}
} }
#undef y int main() {
int i;
scanf("%d%d", &n, &tar);
for (i = ; i <= n; ++i) {
scanf("%d%d", &tr[i].v, &tr[i].fa);
add_edge(tr[i].fa, i);
}
dfs();
for (ans = n - ; ~ans && f[][ans][] < tar; --ans);
printf("%d\n", ans);
return ;
}
BZOJ1722 [Usaco2006 Mar] Milk Team Select 产奶比赛的更多相关文章
- bzoj1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 树形dp
题目链接 bzoj1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 题解 dp[i][j][0 / 1] 以i为根的子数中 相邻点对选了j个的最大价值 代码 #i ...
- 1722: [Usaco2006 Mar] Milk Team Select 产奶比赛
1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 https://www.lydsy.com/JudgeOnline/problem.php?id=1722 分析 ...
- 「BZOJ1722」「Usaco2006 Mar」Milk Team Select产奶比赛 解题报告
Milk Team Select 产奶比赛 Description Farmer John's N (\(1 \le N \le 500\)) cows are trying to select th ...
- [BZOJ1722]Milk Team Select 产奶比赛
Description Farmer John's N (1 <= N <= 500) cows are trying to select the milking team for the ...
- 【Usaco2006Mar】Milk Team Select产奶比赛
[思路分析] 比赛的时候想到了用我确实也想到了树形DP,但是状态没有确定对,连样例都没有过 PS:这是第二道发现还可以用状态作为答案最后输出的题目 正解:树形DP(背包) 按照读进来的数据,我们先建一 ...
- BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1017 Solved: ...
- bzoj1717: [Usaco2006 Dec]Milk Patterns 产奶的模式
后缀数组+二分答案+离散化.(上次写的时候看数据小没离散化然后一直WA...写了lsj师兄的写法. #include<cstdio> #include<cstring> #in ...
- BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式( 二分答案 + 后缀数组 )
二分答案m, 后缀数组求出height数组后分组来判断. ------------------------------------------------------------ #include&l ...
- BZOJ#1717:[Usaco2006 Dec]Milk Patterns 产奶的模式(后缀数组+单调队列)
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的 ...
随机推荐
- python tools: iPython Notebook
Introducing IPython Notebook IPython isn't a different programming language, it's just a set of comp ...
- python_way,day8 面向对象【多态、成员--字段 方法 属性、成员修饰符、特殊成员、异常处理、设计模式之单例模式、模块:isinstance、issubclass】
python_way day8 一.面向对象三大特性: 多态 二.面向对象中的成员 字段.方法属性 三.成员修饰符 四.特殊成员 __init__.__doc__.__call__.__setitem ...
- 关于linux 卸载问题
网上找了一套引擎 非用protocbuff 2.4.1 结果机器上已经装好了2.6.1 网上找了好多办法都行不通 最后终于在一个群里问到 mark一下 例如 我想卸载当前得protoc 那么 第一步 ...
- JZs3c2440裸板程序GPIO操作总结
分别用汇编,汇编+C两种方式 ***************************************汇编编程led_on.s********************************** ...
- 64位Eclipse运行时提示“Failed to load the JNI shared library \Java\jre6\bin\client\jvm.dll”的一个解决方案
系统安装的32位JREE,64位eclipse无法识别,解决方案:下载安装64位jdk即可.
- Redis基础知识之——自定义封装单实例和普通类Redis
一.普通Redis实例化类: class MyRedis { private $redis; public function __construct($host = '121.41.88.209', ...
- web设计经验<三>值得你深入了解的交互设计5大支柱
随着单页式设计和移动端的兴起,网页中的交互设计越来越重要了.为了打造流畅而可靠的用户体验,你需要对交互设计有更加深入的了解. 正如同我们在<交互设计最佳实践(卷1)>中所述,要做好交互设计 ...
- NullPointerException异常的原因??
所谓空指针异常,是因为用空(null)去调用属性或方法. null表示没有这个对象,既然没有这个对象,那么去调用他的属性和方法,就会报异常. <--主要有以下几种原因: 1.使用了未 ...
- C++ Primer 第三章 标准库类型string运算
1. 标准库类型 string string表示可变长的字符序列,使用string必须首先包含string头文件.如何初始化类的对象是由类本身决定的. int n; string s1;//默认初始化 ...
- virtualBox虚拟机安装与主机互访和实现上网配置
1.到官方网下载vitualBox虚拟机,目前最新版本5.0,并安装. https://www.virtualbox.org/ 2.下载安装CentOS系统安装文件 本文采用CentOS-6.5-i3 ...