HDU 1520 Anniversary party

题意:n个人去参加聚会,其中有直接上下级关系的参加聚会会影响气氛,所以不能参加,每个人参加聚会都会有一个活跃度,现在要你计算从n个人中选择出一些人,使得本次聚会的活跃度最大

输入数据:
第一行一个N代表有N个人,然后是N个人每个人的活跃度。
然后再N-1行, 每行两个数字,L K 代表 第K个员工是第L个人的上司。
 
 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9+7;
const int maxn = 6010;
const int MOD = 1e9+7;
int n, dp[2][maxn], In[maxn];///dp[这个点是否选取][第n个点]
vector<vector<int> > G;

void TreeDp(int root)
{
    int len = G[root].size();
    for(int i=0; i<len; i++)
    {
        int v = G[root][i];
        TreeDp(v);
        dp[0][root] += max(dp[0][v],dp[1][v]);
        dp[1][root] += dp[0][v];
    }
}

int main()
{
    int a, b;
    while(scanf("%d", &n) != EOF)
    {
        G.clear();
        G.resize(n+5);
        memset(dp, 0, sizeof(dp));
        memset(In, 0, sizeof(In));
        for(int i=1; i<=n; i++)
            scanf("%d", &dp[1][i]);

        while(scanf("%d %d", &a, &b), a+b)
        {
            G[b].push_back(a);
            In[a] ++;
        }
        int root;

        for(int i=1; i<=n; i++)
            if(!In[i]) root = i;

        TreeDp(root);

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

    }
    return 0;
}

 

posted @ 2015-10-08 16:08  向前走丶不回首  阅读(113)  评论(0编辑  收藏  举报