直接树形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 产奶比赛的更多相关文章

  1. bzoj1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 树形dp

    题目链接 bzoj1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 题解 dp[i][j][0 / 1] 以i为根的子数中 相邻点对选了j个的最大价值 代码 #i ...

  2. 1722: [Usaco2006 Mar] Milk Team Select 产奶比赛

    1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 https://www.lydsy.com/JudgeOnline/problem.php?id=1722 分析 ...

  3. 「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 ...

  4. [BZOJ1722]Milk Team Select 产奶比赛

    Description Farmer John's N (1 <= N <= 500) cows are trying to select the milking team for the ...

  5. 【Usaco2006Mar】Milk Team Select产奶比赛

    [思路分析] 比赛的时候想到了用我确实也想到了树形DP,但是状态没有确定对,连样例都没有过 PS:这是第二道发现还可以用状态作为答案最后输出的题目 正解:树形DP(背包) 按照读进来的数据,我们先建一 ...

  6. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: ...

  7. bzoj1717: [Usaco2006 Dec]Milk Patterns 产奶的模式

    后缀数组+二分答案+离散化.(上次写的时候看数据小没离散化然后一直WA...写了lsj师兄的写法. #include<cstdio> #include<cstring> #in ...

  8. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式( 二分答案 + 后缀数组 )

    二分答案m, 后缀数组求出height数组后分组来判断. ------------------------------------------------------------ #include&l ...

  9. BZOJ#1717:[Usaco2006 Dec]Milk Patterns 产奶的模式(后缀数组+单调队列)

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的 ...

随机推荐

  1. 时间函数 time.h 详解

    C++对时间的操作也有许多值得大家注意的地方.最近,在技术群中有很多网友也多次问到过C++语言中对时间的操作.获取和显示等等的问题.下面,在这篇文章中,笔者将主要介绍在C/C++中时间和日期的使用方法 ...

  2. 输出sed的重定向

    默认情况下,sed编辑器会将进步的结果输出到STDOUT上,你可以在shll脚本中使用所有重订向sed编辑器输出的标准方法. 你可以在脚本中用反引号来将sed编辑器命令的输出重定向到一个变量中供后面使 ...

  3. iOS - UIView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppeara ...

  4. spring DI原理

    什么是IOC? 也称依赖注入 当一个类,需要另一个类的时候,我们不需要再另一个类里进行创建 对象,spring容器会给我们自动的创建 IOC的实现? 通过一定的技术读取spring.xml文件信息,比 ...

  5. git各种撤销操作

    撤销git add: git reset HEAD+路径 或者git reset --+路径 撤销commit: 1.回退到具体的嘻哈值 git reset --hard     2.回退后仍包含本地 ...

  6. hdu4570Multi-bit Trie

    链接 13年长沙邀请赛的题,神题意~ 题意:摘自http://blog.csdn.net/libin56842/article/details/9703457 这题题意确实有点难懂,起码对于我这个英语 ...

  7. ganymedssh2 java执行linux命令

    需要下载ganymed-ssh2-262.jar package ganymed; import java.io.BufferedReader; import java.io.IOException; ...

  8. OpenCV3编程入门笔记(5)重要章节小节及核心函数

  9. genymotion是一款完全超越BlueStacks

    今天给大家推荐一款超赞的神器:genymotion. 一:什么是genymotion      genymotion是一款完全超越BlueStacks的安卓模拟器,正如它中文官网的介绍:快到极致的An ...

  10. Mongos与集群均衡

    版权声明:本文由孔德雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/190 来源:腾云阁 https://www.qclo ...