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;
    }
    
  • 相关阅读:
    使用 Promise.all 同时发起多个请求
    vite 开发 Cesium 程序最佳配置实践
    【linux学习】使用grep命令获取过滤的数据作为下个命令的入参
    记一次k8s depolyment失败处理
    powerdesigner导出excel数据字典
    vue 时间格式
    ASP.NET MVC4 跨域配置
    Win10系统中切换虚拟桌面的快捷键如何设置
    不顾一切最简NHinbernate配置并读写数据库
    Windows time_wait过多解决办法
  • 原文地址:https://www.cnblogs.com/hhlya/p/13443528.html
Copyright © 2011-2022 走看看