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发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的 ...
随机推荐
- 数据库mysql中having 和where的区别
having的用法 having字句可以让我们筛选成组后的各种数据,where字句在聚合前先筛选记录,也就是说作用在group by和having字句前.而 having子句在聚合后对组记录进行筛选. ...
- IP地址匹配
问题描述: 在路由器中,一般来说转发模块采用最大前缀匹配原则进行目的端口查找,具体如下: IP地址和子网地址匹配: IP地址和子网地址所带掩码做AND运算后,得到的值与子网地址相同,则该IP地址与该子 ...
- web设计经验<五>国外设计师总结的7个找灵感实用方法
每个设计师不时会有创意灵感缺失.大脑一片空白的状态.盯着一个空白的屏幕,发愁着“好吧,我现在该做什么呢?该怎么做呢?”有些人喜欢静待,但这不是唯一的一个方法.焦虑的客户或者是你自己的不耐烦,都会让你无 ...
- hdu4570Multi-bit Trie
链接 13年长沙邀请赛的题,神题意~ 题意:摘自http://blog.csdn.net/libin56842/article/details/9703457 这题题意确实有点难懂,起码对于我这个英语 ...
- JSON入门实例
json和XML很像,但它具有更快,更小,阅读性强等优点.不多说,直接来例子: <html><body><h2>通过 JSON 字符串来创建对象</h3> ...
- mysql复制的配置
# 78服务器 server-id=78 # 从哪个数开始 auto_increment_offset=1 # 每次增加多少 auto_increment_increment=2 log-bin=m ...
- CnPlugin 1.5.400
本软件CnPlugin是PL/SQL Developer工具插件,支持PL/SQL Developer 7.0以上版本.增加了PL/SQL Developer工具本身所没有的一些小功能,功能基本一些已 ...
- RecyleView
RecyclerView是经典的ListView的进化与升华,它比ListView更加灵活,但也因此引入了一定的复杂性. 我们知道,ListView通过使用ViewHolder来提升性能.ViewHo ...
- commonJS — 浏览器操作(for Browser)
for Browser github: https://github.com/laixiangran/commonJS/blob/master/src/forBrowser.js 代码 /** * C ...
- php数据通信方式
一:curl$ch = curl_init();curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch,CURLOPT_TIMEOU ...