HDU P1520 Anniversary party

来源:互联网 发布:华师大网络教育好过吗 编辑:程序博客网 时间:2024/05/16 17:43

HDU P1520 Anniversary party


题目

Problem Description
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests’ conviviality ratings.

Input
Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. 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 go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:
L K
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line
0 0

Output
Output should contain the maximal sum of guests’ ratings.

Sample Input

711111111 32 36 47 44 53 50 0

Sample Output

5

题目大意

n 个人,然后,给出 n 个人的开心值 w[i], 最后,给出 tt 不给出)个上下级关系 xy,其中 y 是上级

对于这 n 个人,有直接的上下级关系的人不能同时被选中(必有一个人是没有上下级关系的),求能得到最大的开心值

有多组数据,对于 t 个关系的最后一行,是以 0 0 结尾的,表示关系给出结束


题解

树形DP


代码

#include<cstdio>#include<cstring>using namespace std;int n,tot;int lnk[6005],d[6005],f[6005][2],w[6005];struct edge{    int nxt,y;} e[6005];int readln(){    int x=0,f=1;    char ch=getchar();    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}    while ('0'<=ch&&ch<='9') x=x*10+ch-48,ch=getchar();    return x*f;}int add(int x,int y){    tot++;e[tot].nxt=lnk[x];lnk[x]=tot;e[tot].y=y;}int max(int x,int y){return x>y?x:y;}void dfs(int x){    f[x][0]=0;    f[x][1]=w[x];    for (int i=lnk[x];i;i=e[i].nxt)    {        int y=e[i].y;        dfs(y);        f[x][0]+=max(f[y][0],f[y][1]);        f[x][1]+=f[y][0];    }}int main(){    while(~scanf("%d",&n))    {        memset(d,0,sizeof(d));        memset(f,0,sizeof(f));        memset(lnk,0,sizeof(lnk));        for (int i=1;i<=n;i++) w[i]=readln();        tot=0;        while (true)        {            int x=readln(),y=readln();            if (!x&&!y) break;            add(y,x);            d[x]++;        }        for (int i=1;i<=n;i++)            if (!d[i]) {                dfs(i);                printf("%d\n",max(f[i][0],f[i][1]));                break;            }    }    return 0;}
原创粉丝点击