zoukankan      html  css  js  c++  java
  • 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]));

        }

    }




  • 相关阅读:
    列表的创建02
    python如何设置注释模板,文件模板
    python语言使用rsa密码算法对数据加密时不能对中文加密问题的解决
    python编码解码,字符数据转换问题(自学笔记)
    字符串与数组之间的互相转换
    vc++文本编辑
    OCP 062【中文】考试题库(cuug内部资料)第28题
    OCP 062【中文】考试题库(cuug内部资料)第27题
    OCP 062【中文】考试题库(cuug内部资料)第26题
    OCP 062【中文】考试题库(cuug内部资料)第25题
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617238.html
Copyright © 2011-2022 走看看