探索flutter框架开发的app在移动应用市场的潜力与挑战
537
2022-11-09
Anniversary party (树形dp)
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.
Input Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:
L K
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line
0 0
Output Output should contain the maximal sum of guests' ratings.
Sample Input
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
Sample Output
5
题目大概:
在n个人之间有上司和下属的关系,其实就是树的父亲和孩子,每个人都有权值,孩子和父亲不能同时选择,找出最大的权值。
思路:
之前看过不少博客,这个题有很多做法。
其实这个题最开始是用的直接枚举法找的父亲的孩子是谁,但是超时了,就改为用vector存储父亲的孩子是谁,方便查找和计算,并且用了记忆化,减少用时。
dp[i][0]就是第i个人不被选择,dp[i][1]是第i个人被选择。
那么第i个人有两种情况,第i-1个人被选择的时候,第i个人一定不能选 ans+=dp[i][0];
第i-1个人不被选择的时候,第i个人可选可不选 ans+=max(dp[i][0],dp[i][1]);
代码:
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~