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.
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
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
5
公司n个人开完会,每个人去都会有一个开心值,而l,k有上下级关系,为了大家都愉快,不能出现直系上下级,求最大的开心值得和。
第一个树状dp的题,首先在树上,建树存数据,看了有好几种方法,也试了很多遍,最后感觉最清晰的一个结构,是把父节点子节点都给存了,根据父节点找到根节点root,在根据子节点进行dp。这里的dp公式比较简单:dp[i][1]+=dp[j][0]; dp[i][0]+=max( dp[j][0],dp[j][1])。
代码如下:
dp[i][1]+=dp[j][0];