1. 程式人生 > >POJ 2342 Anniversary party(樹型DP入門題)

POJ 2342 Anniversary party(樹型DP入門題)

題目連結

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

PS:題意:一棵樹每個節點都有自己的權值,現在要求選一些節點使權值之和最大,選節點的要求是要是選了該節點,該節點的父節點和子節點都不能選。

題解:首先宣告一個二維的dp陣列,dp[i][0]表示i節點不選,dp[i][1]表示i節點要選。這樣我們很容易得出狀態轉移方程。

當u節點不選的時候,u節點的子節點都可以選,也可以不選,所以加上兩者當中較大的,dp[u][0]+=max(dp[v][0],dp[v[[1])。(v為u的子節點)

當u節點選的時候,他的子節點就不能選了,dp[u][1]+=dp[v][0];

做的時候我們首先要找到根節點,然後用dfs遍歷。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=6e3+10;
const int mod=10007;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
struct node
{
    int v,next;
} rea[maxn<<1];
int head[maxn],len,root[maxn],dp[maxn][maxn];
void inct()
{
    me(head,-1),len=0;
}
void add(int u,int v)
{
    rea[len].v=v;
    rea[len].next=head[u];
    head[u]=len++;
}
int dfs(int u)//遍歷
{
    for(int i=head[u]; i!=-1; i=rea[i].next)
    {
        int v=rea[i].v;
        dfs(v);
        dp[u][1]+=dp[v][0];
        dp[u][0]+=max(dp[v][1],dp[v][0]);
    }
    return max(dp[u][0],dp[u][1]);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        inct();//每次都要初始化 。
        for(int i=1; i<=n; i++)
        {
            dp[i][0]=0,root[i]=0;//要是給節點不選的話,初始化為0,選的話,初始化為該節點的權值。
            scanf("%d",&dp[i][1]);
        }
        int x,y;
        while(scanf("%d%d",&x,&y)&&x+y)
        {
            add(y,x);///存圖
            root[x]++;
        }
        int ans;
        for(int i=1; i<=n; i++)
            if(!root[i])//找到根節點
                ans=i;
        dfs(ans);
        printf("%d\n",max(dp[ans][0],dp[ans][1]));//因為不確定根節點是否要選,所以直接輸出選或不選中的較大值。
    }
    return 0;
}