POJ-2342 Anniversary party (树形DP)

Anniversary party

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10979   Accepted: 6298

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 N – 1 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

Source

Ural State University Internal Contest October'2000 Students Session

扫描二维码关注公众号,回复: 2743337 查看本文章

借鉴:https://www.cnblogs.com/liyinggang/p/5410491.html

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 6005
using namespace std;
int dp[MAXN][2];
//分析:dp[i][0] 代表第i个人不去,以i为根的子树能够获得的最大愉悦值
//      dp[i][1] 代表第i个人去了,以i为根的子树能够获得的最大愉悦值
//当前结点要么取,要么不取,取时怎么状态转移,不取时怎么状态转移
//dp[i][1] = dp[i][1] + dp[j][0] j为i的孩子
//dp[i][0] = dp[i][0] + max(dp[j][0],dp[j][1])
//要与不要,树上的背包
int indgree[MAXN];
struct Edge
{
   int u,v,last;
}edge[MAXN];
int head[MAXN];
int tot;
void addEdge(int u,int v)
{
    edge[tot].u = u;
    edge[tot].v = v;
    edge[tot].last=head[u];    //这个last可以巧妙的储存以head[u]为起点的所有边(不包括这个起点对应的边)
    head[u]=tot++;             //利用head[u]储存当前u指向的其所有边的最后一条边。
}                              //连锁指向。边指向边
void dfs(int u)
{   
    int i;
    if(head[u]==-1)            //没有指向的点就直接回溯就行
    return;
    for(i=head[u];i!=-1;i=edge[i].last)
    {
        int v = edge[i].v;
        dfs(v);                            //树形DP子问题就是叶子结点的问题,逐渐回溯,实现子问题转换为总问题。
        dp[u][0]+=max(dp[v][0],dp[v][1]);
        dp[u][1]+=dp[v][0];
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(head,-1,sizeof(head));
        memset(dp,0,sizeof(dp));
        memset(indgree,0,sizeof(indgree));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&dp[i][1]);      //dp预处理
            dp[i][0]=0;                 //不去自然是0.
        }
        tot=0;
        int u,v;
        while(scanf("%d%d",&v,&u),u+v)
        {
            addEdge(u,v);
            indgree[v]++;
        }
        int root;
        for(int i=1;i<=n;i++)
        {
            if(indgree[i]==0)
            root=i;        //有向连通图,root取最后一个无入度的为起点
        }
        dfs(root);
        //printf("%d\n",root);
        printf("%d\n",max(dp[root][0],dp[root][1]));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xigongdali/article/details/81529681