POJ 2342——Anniversary party

来源:互联网 发布:php 反射 编辑:程序博客网 时间:2024/05/17 00:54
Anniversary party
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2729 Accepted: 1486

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 N – 1 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

Source

Ural State University Internal Contest October'2000 Students Session
 
/*算法思想:  给一棵树,要求一组节点,节点间没有父子关系,并且使得所有的节点的权值和最大  对每个节点,我们有两种状态:  f[i][0]:不选节点 i ,以 i 为根的子树所能形成的节点集的最大权值和  f[i][1]:选节点 i ,以 i 为根的子树所能形成的节点集的最大权值和  根据状态的定义,不难想出以下状态转移方程:  f[i][0]+=max(f[i_son][0] , f[i_son][1]);  节点 i 不选,那么他的儿子可选可不选  f[i][1]+=f[i_son][0];  节点 i 要选,那么他的儿子必须不选  最后输出 max(f[root][0] , f[root][1]) 就是最后的答案了*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define INF 0x7fffffffusing namespace std;struct data{    int st,en,next;} edge[6005];int val[6005];int f[6005][2],d[6005],in[6005];int head[6005],tot;void add_edge(int st,int en){    edge[tot].st=st;    edge[tot].en=en;    edge[tot].next=head[st];    head[st]=tot++;}void dfs(int v){    if(d[v]==0)    {        f[v][0]=0;        f[v][1]=val[v];        return;    }    for(int i=head[v];i!=-1;i=edge[i].next)    {        dfs(edge[i].en);        f[v][0]+=max(f[edge[i].en][0],f[edge[i].en][1]);        f[v][1]+=f[edge[i].en][0];    }    f[v][1]+=val[v];}int main(){    int n,a,b;    while(scanf("%d",&n),n!=0)    {        for(int i=1;i<=n;i++)            scanf("%d",&val[i]);        memset(head,-1,sizeof(head));        tot=0;        memset(d,0,sizeof(d));        memset(in,0,sizeof(in));        for(int i=1;i<n;i++)        {            scanf("%d%d",&a,&b);            add_edge(b,a);            d[b]++;            in[a]++;        }        int root,ans;        for(int i=1;i<=n;i++)            if(in[i]==0)            {                root=i;                break;            }        memset(f,0,sizeof(f));        dfs(root);        if(n!=1) ans=max(f[root][1],f[root][0]);        else ans=val[1];        if(ans<0) ans=0;        printf("%d\n",ans);    }    return 0;}