zoukankan      html  css  js  c++  java
  • Acwing 285.没有上司的舞会 (树形DP)

    题目

    Ural大学有N名职员,编号为1~N。

    他们的关系就像一棵以校长为根的树,父节点就是子节点的直接上司。

    每个职员有一个快乐指数,用整数 Hi 给出,其中 1≤i≤N。

    现在要召开一场周年庆宴会,不过,没有职员愿意和直接上司一起参会。

    在满足这个条件的前提下,主办方希望邀请一部分职员参会,使得所有参会职员的快乐指数总和最大,求这个最大值。

    输入格式
    第一行一个整数N。

    接下来N行,第 i 行表示 i 号职员的快乐指数Hi。

    接下来N-1行,每行输入一对整数L, K,表示K是L的直接上司。

    输出格式
    输出最大的快乐指数。

    数据范围
    1≤N≤6000,
    −128≤Hi≤127
    输入样例:
    7
    1
    1
    1
    1
    1
    1
    1
    1 3
    2 3
    6 4
    7 4
    4 5
    3 5
    输出样例:
    5

    思路

    考虑树的递归特性,并且分析状态转移,那么大的状态一定是根据子树转移过来,邻接表存树进行dp就好了。

    代码实现

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=0;i<end;i++)
    #define inf 0x3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    typedef pair<int ,int> PII;
    typedef pair<int ,PII> PIII;
    ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
    inline int read() {
        char ch=getchar(); int x=0, f=1;
        while(ch<'0'||ch>'9') {
            if(ch=='-') f = -1;
            ch=getchar();
        } 
        while('0'<=ch&&ch<='9') {
            x=x*10+ch-'0';
            ch=getchar();
        }   return x*f;
    }
    
    const int maxn=6010;
    vector <int > G[maxn];
    int w[maxn];
    int n;
    int st[maxn];
    int f[maxn][2];
    
    void dfs (int u) {
        f[u][1]=w[u];
        rev (i,0,G[u].size()) {
            int j=G[u][i];
            dfs (j);
            f[u][0]+=max (f[j][0],f[j][1]);
            f[u][1]+=f[j][0];
        }
    }
    
    int main () {
        cin>>n;
        rep (i,1,n) {
            cin>>w[i];
        }
        rep (i,1,n-1) {
            int a,b;
            cin>>a>>b;
            G[b].pb (a);
            st[a]=1;
        }
        int root=1;
        while (st[root]) root++;
    
        dfs (root);
        cout<<max (f[root][1],f[root][0])<<endl;
    
        return 0;
    }
    
  • 相关阅读:
    poj 1743 Musical Theme 后缀数组
    poj 1743 Musical Theme 后缀数组
    cf 432D Prefixes and Suffixes kmp
    cf 432D Prefixes and Suffixes kmp
    hdu Data Structure? 线段树
    关于position和anchorPoint之间的关系
    ios POST 信息
    CALayers的代码示例
    CALayers详解
    ios中得sqlite使用基础
  • 原文地址:https://www.cnblogs.com/hhlya/p/13443528.html
Copyright © 2011-2022 走看看