zoukankan      html  css  js  c++  java
  • 143. Long Live the Queen 树形dp 难度:0

    143. Long Live the Queen

    time limit per test: 0.25 sec. 
    memory limit per test: 4096 KB

     

    The Queen of Byteland is very loved by her people. In order to show her their love, the Bytelanders have decided to conquer a new country which will be named according to the queen's name. This new country contains N towns. The towns are connected by bidirectional roads and there is exactly ONE path between any two towns, walking on the country's roads. For each town, the profit it brings to the owner is known. Although the Bytelanders love their queen very much, they don't want to conquer all the N towns for her. They will be satisfied with a non-empty subset of these towns, with the following 2 properties: there exists a path from every town in the subset to every other town in the subset walking only through towns in the subset and the profit of the subset is maximum. The profit of a subset of the N towns is equal to the sum of the profits of the towns which belong to the subset. Your task is to find the maximum profit the Bytelanders may get.

     

    Input

    The first line of input will contain the number of towns N (1<=N<=16 000). The second line will contain N integers: the profits for each town, from 1 to N. Each profit is an integer number between -1000 and1000. The next N-1 lines describe the roads: each line contains 2 integer numbers a and b, separated by blanks, denoting two different towns between which there exists a road.

     

    Output

    The output should contain one integer number: the maximum profit the Bytelanders may get.

     

    Sample Input

    5
    -1 1 3 1 -1
    4 1
    1 3
    1 2
    4 5
    

    Sample Output

    4

    题意:求一棵收益最大的树/子树,不能为空
    思路:分别对每个节点维护以该节点为根所能得到的最大收益,更新答案即可
    转移方程dp[i]=sum(dp[son[i]]>0?dp[son[i]]:0)

    #include <cstdio>
    #include <cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=16001;
    const int maxm=32001;
    int first[maxn],next[maxm],to[maxm],profit[maxn],len,n;
    int sum[maxn];
    void addedge(int f,int t){
        next[len]=first[f];
        first[f]=len;
        to[len]=t;
        swap(f,t);len++;
        next[len]=first[f];
        first[f]=len;
        to[len]=t;
        len++;
    }
    int dfs(int s,int f){
        sum[s]=profit[s];
        for(int p=first[s];p!=-1;p=next[p]){
            int t=to[p];
            if(t==f)continue;
            int son=dfs(t,s);
            if(son>0)sum[s]+=son;
        }
        return sum[s];
    }
    int main(){
        scanf("%d",&n);
        int tf,tt;
        memset(first,-1,sizeof(first));
        for(int i=1;i<=n;i++)scanf("%d",profit+i);
        for(int i=1;i<n;i++){scanf("%d%d",&tf,&tt);addedge(tf,tt);}
        dfs(1,-1);
        int maxn=-0x7ffffff;
        for(int i=1;i<=n;i++){maxn=max(maxn,sum[i]);}
        printf("%d
    ",maxn);
    }
    

      

  • 相关阅读:
    Redhat各个版本和内核对照
    Java8 lambda表达式总结
    conda 安装指定版本的指定包
    git初始化的几句shell
    MYsqli 绑定插入与查询实例
    按天去除重复数据,为0则取0,否则取最大的那个值
    存储过程,循环插入1000条记录
    主表如何统计在附表中的出现次数?
    Invalid argument supplied for foreach()
    二十、mysql mysqldump备份工具
  • 原文地址:https://www.cnblogs.com/xuesu/p/4064657.html
Copyright © 2011-2022 走看看