Anniversary Party

来源:互联网 发布:微店怎么绑定淘宝店铺 编辑:程序博客网 时间:2024/04/30 05:45

 Anniversary Party

Time limit: 0.5 second
Memory limit: 8 MB

Background

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.

Problem

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

Input

The first line of the input contains a number N. 1 ≤ N ≤ 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

Output

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

Sample

inputoutput
711111111 32 36 47 44 53 50 0
5


#include<cstdio>#include<cstring>#include<vector>using namespace std;#define N 6010int n;int dp[N][2];     /**dp[i][0]表示不选择i点时,i点及其子树能选出的最大值;dp[i][1]表示选择i点时,i点及其子树能选出的最大值**/vector<int> c[N];/**c[j][k]为j点的第k个孩子**/int v[N];         /**用于寻找根节点**/void TreeDp(int x)      /**树形DP**/{    for(int i=0; i<c[x].size(); i++)    {        int y=c[x][i];        TreeDp(y);        dp[x][1]+=dp[y][0];       /**当取节点x时,dp的值等于x的子树(不取x的孩子)的dp值的和+自己的欢乐值**/        dp[x][0]+=max(dp[y][0],dp[y][1]);/**当不取节点x时,dp的值等于x的所有子树(x的孩子节点取或不取)的dp值的最大值之和**/    }}void read(){    int i,a,b;    scanf("%d",&n)!=EOF;    memset(dp,0,sizeof(dp));    memset(v,0,sizeof(v));    for(i=1; i<=n; ++i)        scanf("%d",&dp[i][1]);    while(scanf("%d%d",&a,&b)!=EOF)    {        if((a+b)==0)            break;        c[b].push_back(a);        v[a]=1;  /**凡是有父亲节点的,都标记为1,根节点没有父亲节点,标记是0**/    }    for(i=1; i<=n; i++)        if(v[i]==0)       /**找出了根节点**/        {            TreeDp(i);            break;        }    printf("%d\n",max(dp[i][0],dp[i][1]));}int main(){    read();    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 广工期末考试被老师警告了怎么办 我在农村不当队长我能力不够怎么办 我在农村不当队长我当代表怎么办 北京本地人社保中间断了一年怎么办 我想把大哥的孩子带出国要怎么办 美国有亲戚想让孩子出国怎么办 连接温控器的线断了怎么办 植发后好多原生发都脱落了怎么办 4岁宝宝支体能力差怎么办 当与游客发生矛盾时员工怎么办 顾客与同事发生矛盾你该怎么办 当一个人犯下的过错难以原谅怎么办 开车把别人撞成植物人了该怎么办 结婚证丢了孩子上不上学了怎么办 办房产证前加户主姓名怎么办手续 孩子上学户口跟房产不在一起怎么办 我家小孩被别的家长打了怎么办 要是小孩骗家长说老师打他怎么办 孩子在学校被家长打小孩怎么办 小孩在学校被老师打淤青家长怎么办 小孩和家长一吵架就说死怎么办 王者荣耀号被别人家长联接了怎么办 儿子12岁总是跟大人顶嘴怎么办 课堂上有学生和你顶嘴你怎么办 小孩看到大人吵架就哭了 怎么办 五年级的孩子叛逆爱发脾气怎么办 6个月孩子多动怎么办呀 小孩被大人打了很生气该怎么办 小孩在学校调皮被老师打了怎么办 四岁宝宝咳嗽半个月了怎么办 三岁宝宝咳嗽半个月了还不好怎么办 孩子在幼儿园不听话天天罚站怎么办 孩子个性太强脾气太倔不听话怎么办 孩子有十七了太不听话了怎么办 小孩的学籍填错了怎么办还能改吗 生完孩子后脾气暴躁易怒怎么办 生完孩子之后变懒了怎么办呀? 孩子三年级了成绩不突出家长怎么办 自私势力的父母想把我害死怎么办 父母如果养出自私的孩子怎么办 大人得了地图舌怎么办要怎么治疗