zoukankan      html  css  js  c++  java
  • 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
    ", max(dp[0][root],dp[1][root]));
    
        }
        return 0;
    }
  • 相关阅读:
    sortColors
    搜索二维矩阵
    矩阵置零
    求解最大子数组的和
    length of the longest substring without repeating character
    求解最大回文子字符串
    求两数的和
    验证回文串的一点解题思路
    fiddler-10-结合 Mock.js 伪接口数据进行测试(下)
    fiddler-9-结合 Mock.js 伪接口数据进行测试(上)
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4861339.html
Copyright © 2011-2022 走看看