直接树形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. POJ 3104 Drying(二分答案)

    题目链接:http://poj.org/problem?id=3104                                                                  ...

  2. Log4j XML 配置

    Xml代码 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configurat ...

  3. java乱码问题(转)

    参考: http://blog.csdn.net/beijiguangyong/article/details/7414247 http://www.zhihu.com/question/202126 ...

  4. SpringMVC后缀

    <!-- 在这里,使用*.html为后缀的URL都能被baobaotao Servlet截获,进而转由SpringMVC框架进行处理.在 Struts框架中,一般将URL后缀配置为*.do:在w ...

  5. linux 目录结构图解

    参考资料:http://www.linuxidc.com/Linux/2016-08/134701.htm

  6. hostapd源代码分析(三):管理帧的收发和处理

    hostapd源代码分析(三):管理帧的收发和处理 原文链接:http://blog.csdn.net/qq_21949217/article/details/46004379 这篇文章我来讲解一下h ...

  7. Android 热修复Nuwa的原理及Gradle插件源码解析

    现在,热修复的具体实现方案开源的也有很多,原理也大同小异,本篇文章以Nuwa为例,深入剖析.  Nuwa的github地址 https://github.com/jasonross/Nuwa 以及用于 ...

  8. TCP/IP 小知识

    子网掩码有数百种,这里只介绍最常用的两种子网掩码,它们分别是“255.255.255.0”和“255.255.0.0”. 1.子网掩码是“255.255.255.0”的网络:最后面一个数字可以在0~2 ...

  9. HashMap详解

    HashMap:http://www.cnblogs.com/xwdreamer/archive/2012/06/03/2532832.html ConcurrentHashMap:http://ww ...

  10. ARM-linux嵌入式开发平台搭建1

    初学嵌入式开发,由于是自学,走了很多弯路,现总结一下嵌入式ARM-LINUX开发环境搭建步骤: 1.安装linux系统,由于初学,我选择fedora 14.安装的具体步骤就不详细说了. 2.安装NFS ...