POJ 2342——Anniversary party

Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total 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

7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 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 0x7fffffff
using 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;
}

你可能感兴趣的:(tree,Integer,input,each,structure,Forms)