Anniversary party 树形dp

简单的树形dp

dp[i] 表示以i为根的包括i 的最大欢乐度

f[i] 表示以i为根不包括i 的最大欢乐度

叶节点则为 f[i] = 0, dp[i] = w[i]  只要初始化 dp f 都全为0 就可以了 dfs会自动复制

说实话我觉得这题很不严谨 他说欢乐度可以为负值  按理来说如果小于0 就应该将欢乐度设为0 表示那个人不参加

最后是 无论设不设为0 都AC 证明根本没有欢乐度为负值的

要注意的是

1. 不是只有一个case  我一开始以为只有一个case  一直错 要处理到没有输入为止。。。 题目又没说明

2. 有可能有多棵树 所以要多次dfs()

View Code
Anniversary party

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
View Code
#include <stdio.h>
#include <algorithm>
#include <string.h>

const int MAXN = 6005;
struct node
{
    int index ;
    node *next ;
}adj[MAXN];
bool vis[MAXN], isRoot[MAXN];
int n, m, f[MAXN], dp[MAXN], w[MAXN]; 
void addEdge(int x, int y)
{
    node *p = new node;
    p->index = y;
    p->next = adj[x].next;
    adj[x].next = p;
    isRoot[y] = 0;
}
int max(int a, int b)
{    return a>=b ?a :b  ;  }
void dfs(int root)
{
    vis[root] = 1;
    node *p = adj[root].next;
    while( p != NULL )
    {
        int u = p->index;
        if(!vis[u])
        {
            dfs(u);
        //    dp[root] += f[u]<0 ?0 :f[u];
        //    int tmp = max(dp[u], f[u]);
        //    f[root] += tmp < 0 ? 0 : tmp ;
            dp[root] += f[u];
            f[root] += max(dp[u], f[u]);
        }
        p = p->next;
    }
    dp[root] += w[root];
}
int main()
{
    int i, a, b, root, ans = 0;
    while(scanf("%d", &n)!=EOF)
    {
        ans = 0;
        for(i=1; i<=n; i++)
        {
            scanf("%d", &w[i]);
            isRoot[i] = 1; dp[i] = f[i] = 0;
            adj[i].next = NULL, vis[i] = 0;
        }
        while(scanf("%d %d", &a, &b), a|b)
            addEdge(b, a);
        for(i=1; i<=n; i++)
            if(isRoot[i])
            {
                dfs(i);
                ans += max(dp[i], f[i]);
            }
            printf("%d\n", ans);
    }
    return 0;
}

 

 

posted @ 2013-03-20 10:55  April_Tsui  阅读(155)  评论(0编辑  收藏  举报