zoukankan      html  css  js  c++  java
  • 【URAL1039】Anniversary Party

    题目链接

    Anniversary Party

    题目背景

    The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor relation forms a tree rooted at the president. Employees are numbered by integer numbers in a range from (1) to (N), The personnel office has ranked each employee with a conviviality rating. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.

    题目描述

    Your task is to make up a guest list with the maximal conviviality rating of the guests.

    输入格式

    The first line of the input contains a number (N). (1 le N le 6000). Each of the subsequent (N) lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from (-128) to (127). After that the supervisor relation tree goes. Each line of the tree specification has the form
    (<L> <K>)
    which means that the (K)-th employee is an immediate supervisor of (L)-th employee. Input is ended with the line
    (0 0)

    输出格式

    The output should contain the maximal total rating of the guests.

    样例输入

    7
    1
    1
    1
    1
    1
    1
    1
    1 3
    2 3
    6 4
    7 4
    4 5
    3 5
    0 0
    

    样例输出

    5
    

    题解

    题意:有一棵树,每个点有一个权值,可以选择一些点,要求一个点和它父亲不能同时被选择,求选择的点的权值之和最大为多少。
    很裸的树上dp,我们令(dp[i][1/0])表示(i)这个点选或不选,以(i)为根的子树内权值之和最大为多少。
    转移方程就是:
    (dp[i][1]=sum dp[j][0])
    (dp[i][0]=sum max)((dp[j][0],dp[j][1]))
    (j)(i)的儿子。
    上代码:

    #include<bits/stdc++.h>
    using namespace std;
    int n;
    int x,y;
    int a[6009];
    int dp[6009][2];
    struct aa{
        int to,nxt;
    }p[20009];
    int h[6009],len=1;
    void add(int u,int v){
        p[++len].to=v;
        p[len].nxt=h[u];
        h[u]=len;
    }
    void dfs(int u,int fa){
        int sum=0,ss=0;
        dp[u][0]=0;
        dp[u][1]=a[u];
        for(int j=h[u];j;j=p[j].nxt){
            if(p[j].to!=fa){
                dfs(p[j].to,u);
                dp[u][0]+=max(dp[p[j].to][0],dp[p[j].to][1]);
                dp[u][1]+=dp[p[j].to][0];
            }
        }
    }
    int main(){
        scanf("%d",&n);
        for(int j=1;j<=n;j++)
            scanf("%d",&a[j]);
        while(1){
            scanf("%d%d",&x,&y);
            if(x==0 && y==0) break;
            add(x,y);
            add(y,x);
        }
        dfs(1,0);
        printf("%d",max(dp[1][0],dp[1][1]));
        return 0;
    }
    
  • 相关阅读:
    CF1080D Olya and magical square
    CF1062D Fun with Integers
    leetcode378 Kth Smallest Element in a Sorted Matrix
    hdoj薛猫猫杯程序设计网络赛1003 球球大作战
    hihocoder1068 RMQ-ST算法
    hihocoder1032 最长回文子串
    hihocoder1394 网络流四·最小路径覆盖
    hihocoder1398 网络流五·最大权闭合子图
    【bzoj4562】HAOI2016食物链
    【bzoj1026】windy数
  • 原文地址:https://www.cnblogs.com/linjiale/p/13478005.html
Copyright © 2011-2022 走看看