Description

Farmer John's N (1 <= N <= 500) cows are trying to select the milking team for the world-famous Multistate Milking Match-up (MMM) competition. As you probably know, any team that produces at least X (1 <= X <= 1,000,000) gallons of milk is a winner. Each cow has the potential of contributing between -10,000 and 10,000 gallons of milk. (Sadly, some cows have a tendency to knock over jugs containing milk produced by other cows.) The MMM prides itself on promoting family values. FJ's cows have no doubt that they can produce X gallons of milk and win the contest, but to support the contest's spirit, they want to send a team with as many parent-child relationships as possible (while still producing at least X gallons of milk). Not surprisingly, all the cows on FJ's farm are female. Given the family tree of FJ's cows and the amount of milk that each would contribute, compute the maximum number of parent-child relationships that can exist in a winning team. Note that a set of cows with a grandmother-mother-daughter combination has two parent-child relationships (grandmother-mother, mother-daughter).

约翰的N(1≤N≤500)头奶牛打算组队去参加一个世界级的产奶比赛(Multistate Milking

Match-up,缩写为MMM).她们很清楚其他队的实力,也就是说,她们派出的队只要能产出至少X(I≤X≤1000000)加仑牛奶,就能赢得这场比赛.    每头牛都能为集体贡献一定量的牛奶,数值在-10000到10000之间(有些奶牛总是想弄翻装着其他奶牛产的奶的瓶子).

MMM的举办目的之一,是通过竞赛中的合作来增进家庭成员之间的默契.奶牛们认为她们总是能赢得这场比赛,但为了表示对比赛精神的支持,她们希望在选出的队伍里能有尽可能多的牛来自同一个家庭,也就是说,有尽可能多对的牛有直系血缘关系(当然,这支队伍必须能产出至少X加仑牛奶).当然了,所有的奶牛都是女性,所以队伍里所有直系血亲都是母女关系.    约翰熟知所有奶牛之间的血缘关系.现在他想知道,如果在保证一支队伍能赢得比赛的情况下,队伍中最多能存在多少对血缘关系.注意,如果一支队伍由某头奶牛和她的母亲、她的外祖母组成,那这支队伍里一共有2对血缘关系(这头奶牛外祖母与她的母亲,以及她与她的母亲).

Input

* Line 1: Two space-separated integers, N and X. * Lines 2..N+1: Line i+1 contains two space-separated integers describing cow i. The first integer is the number of gallons of milk cow i would contribute. The second integer (range 1..N) is the index of the cow's mother. If the cow's mother is unknown, the second number is 0. The family information has no cycles: no cow is her own mother, grandmother, etc.

第1行:两个用空格隔开的整数N和X.

第2到N+1行:每行包括两个用空格隔开的整数,第一个数为一只奶牛能贡献出的牛奶的加仑数,第二个数表示她的母亲的编号.如果她的母亲不在整个牛群里,那第二个数为0.并且,血缘信息不会出现循环,也就是说一头奶牛不会是自己的母亲或祖母,或者更高代的祖先.

Output

* Line 1: The maximum number of parent-child relationships possible on a winning team. Print -1 if no team can win.

输出在一个能获胜的队伍中,最多可能存在的有血缘关系的牛的对数.如果任何一支队伍都不可能获胜,输出-1.

Sample Input

5 8
-1 0 //第一个数字代表这头奶的产量,第二个数字代表其父亲点是哪一个.
3 1
5 1
-3 3
2 0

INPUT DETAILS:

There are 5 cows. Cow 1 can produce -1 gallons and has two daughters, cow
2 and 3, who can produce 3 and 5 gallons, respectively. Cow 3 has a
daughter (cow 4) who can produce -3 gallons. Then there's cow 5, who can
produce 2 gallons.

Sample Output

2

HINT

约翰一共有5头奶牛.第1头奶牛能提供-1加仑的牛奶,且她是第2、第3头奶牛的母亲.第2、第3头奶牛的产奶量务别为3加仑和5加仑.第4头奶牛是第3头奶牛的女儿,她能提供-3加仑牛奶.还有与其他牛都没有关系的第5头奶牛,她的产奶量是2加仑.最好的一支队伍包括第1,2,3,5头奶牛.她们一共能产出(-1)+3+5+2=9≥8加仑牛奶,并且这支队伍里有2对牛有血缘关系(1—2和1-3).如果只选第2,3,5头奶牛,虽然总产奶量会更高(10加仑),但这支队伍里包含的血缘关系的对数比上一种组合少(队伍里没有血缘关系对).

树形dp

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#define M 501
using namespace std; struct point{int to,next;}e[M<<];
int n,num,m,ans=-;
int head[M],c[M],dp[M][M][]; void add(int from,int to)
{
e[++num].next=head[from];
e[num].to=to;
head[from]=num;
} void dfs(int x)
{
dp[x][][]=c[x],dp[x][][]=;
if(!x) dp[x][][]=-;
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
dfs(to);
for(int j=n;j>=;j--)
for(int k=;k<=j;k++)
{
if(k) dp[x][j][]=max(dp[x][j][],dp[x][j-k][]+dp[to][k-][]);
dp[x][j][]=max(dp[x][j][],dp[x][j-k][]+dp[to][k][]);
dp[x][j][]=max(dp[x][j][],dp[x][j-k][]+max(dp[to][k][],dp[to][k][]));
}
}
} int main()
{
memset(dp,0xc0,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=,fa;i<=n;i++)
{
scanf("%d%d",&c[i],&fa);
add(fa,i);
}
dfs();
for(int i=;i<=n;i++)
if(dp[][i][]>=m)
ans=i;
printf("%d",ans);
return ;
}

[BZOJ1722]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. 「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 ...

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

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

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

    直接树形dp就好了恩 令$f[i][j][t]$表示以$i$为根的子树,选出来的点存在$j$对父子关系,$t$表示$i$这个点选或者没选,的最大产奶值 分类讨论自己和儿子分别有没有选,然后转移一下就好 ...

  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. 【BZOJ-1717】Milk Patterns产奶的模式 后缀数组

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

  8. 【BZOJ】【1717】【USACO 2006 Dec】Milk Patterns产奶的模式

    后缀数组 o(︶︿︶)o 唉傻逼了一下,忘了把后缀数组的字典范围改回20001,直接21交了上去,白白RE了两发……sigh 既然要找出现了K次的子串嘛,那当然要用后缀数组了>_>(因为我 ...

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

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

随机推荐

  1. Python全栈day21(调用模块路径BASEDIR的正确方法)

    正常写python程序会有一个可执行的bin.py文件,假如这个文件需要导入my_module里面定义的模块,应该怎么设置sys.path 文件夹目录结构如下,因为bin不在与my_module同级目 ...

  2. MySQL 监控指标

    为了排查问题,对数据库的监控是必不可少的,在此介绍下 MySQL 中的常用监控指标. 简介 MySQL 有多个分支版本,常见的有 MySQL.Percona.MariaDB,各个版本所对应的监控项也会 ...

  3. python(五)常用模块学习

    版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明. https://blog.csdn.net/fgf00/article/details/52357 ...

  4. 【react路由】react 路由被自动加了个#

    路由自动加#是由hashhistory造成: https://segmentfault.com/q/1010000012097148 单页面应用 前端跳转 or 服务器跳转: https://my.o ...

  5. 荣誉墙项目day28 django常用函数

    1.在网页上渲染字符串from django.http import HttpResponsereturn HttpResponse(u"hello world") 2.渲染网页f ...

  6. PHP error_log 新认知

    //error_log 简介及使用方法 //  error_log("消息","类型","路径"); //message   //type  ...

  7. 【使用时发生的意外】file is not sufficiently replicated yet

    异常堆栈如下: -- ::, ERROR [com.ultrapower.secsight.util.HdfsUtil] - 追加写入文件失败! org.apache.hadoop.ipc.Remot ...

  8. WCF服务对于处理客户端连接的一点思考

    对于每个客户端的,服务端是否为每个客户端有专门的“通道”? 目的:想在服务端记录下来客户端的访问记录(进入.各个操作.离开等信息),并将其执行的操作独立记录在各个客户端对应的日志中. 下面是代码: 契 ...

  9. 正则表达式验证合法的IP地址

    IPv4地址 最初设计互联网络时,为了便于寻址和层次化构造网络,每个IP地址包括两个标识码(ID),即网络ID和主机ID.同一个物理网络上的所有主机都使用同一个网络ID,网络上的一个主机(包括网络上的 ...

  10. genymotion——VT-x is not available (VERR_VMX_NO_VMX) 的解决方案

    进入虚拟机设置页面