HDU 1520 Anniversary party【树形DP】

Problem Description
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

思路

和经典的树形DP简介上司差不多,我的第一道树形DP,看了一个大牛的博客才有点印象,树形DP大概怎么写。思路是:每个人的状态只有两种,取或者不取。因此从叶子节点往根遍历,对于每个节点有状态方程如下:(1代表取,0代表不取)

dp[i][1]+=dp[i.son][0];

dp[i][0]+=max(dp[i.son][1], dp[i.son][0]);

源码

#include<stdio.h>

#include<string.h>

#include<iostream>

#include<vector>

using namespace std;

int root, lovely[6005];

vector<int>V[6005];

int dp[6005][3];

void dfs(int rr)

{

    int len=V[rr].size(), i, j;

    if(len==0)

    {

        dp[rr][0]=0;

        dp[rr][1]=lovely[rr];

        return ;

    }

    for(i=0; i<len; i++)

    {

        dfs(V[rr][i]);

        dp[rr][0]=0;

        dp[rr][1]=lovely[rr];

        for(j=0; j<len; j++)

        {

                     dp[rr][0]+=max(dp[V[rr][j]][1], dp[V[rr][j]][0]);

                     dp[rr][1]+=dp[V[rr][j]][0];

        }

    }

}

int main()

{

    int n, i, j, son[6005], l, k;

    while(scanf("%d", &n)!=EOF)

    {

        for(i=0; i<=6000; i++)

            V[i].clear();

        memset(son, 0, sizeof(son));

        for(i=1; i<=n; i++)

            scanf("%d", &lovely[i]);

        for(i=1; i<=n; i++)

        {

            scanf("%d%d", &l, &k);

            if(l==0&&k==0)

                break;

            son[l]=1;

            V[k].push_back(l);

        }

        for(i=1; i<=n; i++)

            if(!son[i])

            {

                root=i;

                break;

            }

        memset(dp, 0, sizeof(dp));

        dfs(root);

        printf("%d\n", max(dp[root][1], dp[root][0]));

    }

}




你可能感兴趣的:(part)