HDU2412 树状DP
来源:互联网 发布:信息搜集软件 编辑:程序博客网 时间:2024/05/17 02:45
Party at Hali-Bula
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 138 Accepted Submission(s): 51
Problem Description
Dear Contestant,
I'm going to have a party at my villa at Hali-Bula to celebrate my retirementfrom BCM. I wish I could invite all my co-workers, but imagine how an employeecan enjoy a party when he finds his boss among the guests! So, I decide not toinvite both an employee and his/her boss. The organizational hierarchy at BCMis such that nobody has more than one boss, and there is one and only oneemployee with no boss at all (the Big Boss)! Can I ask you to please write aprogram to determine the maximum number of guests so that no employee isinvited when his/her boss is invited too? I've attached the list of employeesand the organizational hierarchy of BCM.
Best,
--Brian Bennett
P.S. I would be very grateful if your program can indicate whether the list ofpeople is uniquely determined if I choose to invite the maximum number ofguests with that condition.
Input
The input consists of multiple test cases. Each test caseis started with a line containing an integer n (1 ≤ n ≤ 200), the number of BCMemployees. The next line contains the name of the Big Boss only. Each of thefollowing n-1 lines contains the name of an employee together with the name ofhis/her boss. All names are strings of at least one and at most 100 letters andare separated by blanks. The last line of each test case contains a single 0.
Output
For each test case, write a single line containing anumber indicating the maximum number of guests that can be invited according tothe required condition, and a word Yes or No, depending on whether the list ofguests is unique in that case.
Sample Input
6
Jason
Jack Jason
Joe Jack
Jill Jason
John Jack
Jim Jill
2
Ming
Cho Ming
0
Sample Output
4 Yes
1 No
给定一棵关系树,从中选择一些点,使这些点均不存在亲子关系,最多能取多少个点,并且判断取法是否唯一.
经典的树状DP.
设dp[i][0]为在以i为根的子树中,不选择点i最多能够选的数目,dp[i][1]为选择i点的最多数目.
状态转移方程:
当i为叶子节点时:
dp[i][0]=0;
dp[i][1]=1;
当i为非叶子节点时:
dp[i][0]=sum(max(dp[j][0],dp[j][1])) (j为i的儿子)
dp[i][1]=sum(dp[j][0]) (j为i的儿子)
解的唯一性的判断:
设u[i][x]为0时表示dp[i][x]的解唯一,为1则表示不唯一.
当x为0时,若存在j是i的儿子,使得dp[j][0]>dp[j][1]且u[j][0]=1,或dp[j][0]<dp[j][1]且u[j][1]=1或dp[j][0]=dp[j][1],则u[i][0]=1;
当x为1时,若存在j是i的儿子,使得u[j][0]=1,则u[i][0]=1;
代码如下:
- HDU2412 树状DP
- hdu2412 树形DP
- hdu2412 树形DP
- hdu2412
- HDU2412 && POJ3342:Party at Hali-Bula(树形DP)
- HDU2412 & POJ3342 Party at Hali-Bula_树形DP
- hdu2412:Party at Hali-Bula map应用&&树形DP
- hdu2412(树形dp+判断解的唯一性)
- HDU2412 Party at Hali-Bula(树形DP)
- 树状DP
- 树状DP
- 树状DP
- poj3324--hdu2412--Party at Hali-Bula(树形dp,结果是否唯一)
- 选课 树状DP
- HDU1054 树状DP
- 【树状DP】ural 1018
- hdu 3586 树状dp
- hdu 1520 树状 dp
- jQuery jqGrid 文档
- HDU1069 最长单调子序列变形
- HDU1054 树状DP
- 重载、虚函数(覆盖、多态)、隐藏 -- 代码示例
- 一些关于Java面向对象学习、培训机构培训、大学生活状态的感悟和总结
- HDU2412 树状DP
- 今天应该是易语言十周年纪念日(2000/09/16 - 2010/09/16)
- dsa
- [IE9] IE9 beta版下载链接
- PSP 2000-How to Play Dominoes - Mexican Train Domino Rules
- 我们是不是该有脾气
- [IE9] 如何让你的网站在IE9和Win7任务栏上更闪亮
- OOP初探
- 匈牙利命名法-简介