hdu 1520 Anniversary party

A - Anniversary party
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status

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

      
      
      
      
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
题目意思,就是一个关系树,很好的树状dp入门题,这就要求我们,不想写,只好用了vector,结果比别人多了100多ms,刚形始还以为是多颗树呢!得多看看题啊
#include <iostream>
#include<vector>
#include<string.h>
#include<stdio.h>
using namespace std;
int n,l,k;
int dp[2][6600],visit[6600];
vector<int > p[6600];
int ffmax(int a,int b)
{
    if(a>b)
    return a;
    return b;
}
void dfs(int x)//找到这个点所有所连的点的最大值
{
    int y;
    for(int i=0;i<p[x].size();i++)
    {
        y=p[x][i];
        dfs(y);//先把子问题搜索出来
        dp[0][x]+=ffmax(dp[0][y],dp[1][y]);//当前这个人不取,就为下一个结点取,或者不取的最大值
        dp[1][x]+=dp[0][y];//如果这个结点取了,下属,就一定不能取了
    }
}
int main()
{

    int i,l,k,end,j;
    while(scanf("%d",&n)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        memset(visit,0,sizeof(visit));
        for(i=1;i<=n;i++)
        {
             scanf("%d",&dp[1][i]);
             p[i].clear();//清空
        }

        while(1)
        {
            scanf("%d%d",&l,&k);
            if(l==0&&k==0)
            break;
            visit[l]=1;//标记l一定不是根结点
            p[k].push_back(l);
        }
        for(i=1;i<=n;i++)
        {


            if(visit[i]!=1)//找到根结点
            {
                //printf("%d end",i);
                end=i;
                dfs(i);
                break;
            }

        }
        

        printf("%d\n",ffmax(dp[0][end],dp[1][end]));
    }
    return 0;
}


你可能感兴趣的:(树状DP)