HDU 1520 Anniversary party(簡單樹形DP)


Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2124    Accepted Submission(s): 896


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
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
 

 

Recommend
linle
 
 
簡單的樹形DP入門題
分別用STL中的vector建了個有向圖
然后又用結構體建了個無向圖。
兩個程序
/*
HDU 1540
簡單樹形DP
STL中的vector實現鏈表建樹

G++ 125 ms 784K
*/

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=6050;
vector<int>vec[MAXN];
int f[MAXN];
int hap[MAXN];
int dp[MAXN][2];

void dfs(int root)
{
    int len=vec[root].size();
    dp[root][1]=hap[root];
    for(int i=0;i<len;i++)
       dfs(vec[root][i]);
    for(int i=0;i<len;i++)
    {
        dp[root][0]+=max(dp[vec[root][i]][1],dp[vec[root][i]][0]);
        dp[root][1]+=dp[vec[root][i]][0];
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    int a,b;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&hap[i]);
            vec[i].clear();
            f[i]=-1;//樹根標記
            dp[i][0]=dp[i][1]=0;
        }
        while(scanf("%d%d",&a,&b))
        {
            if(a==0&&b==0)break;
            f[a]=b;
            vec[b].push_back(a);
        }
        a=1;
        while(f[a]!=-1)a=f[a];//找到樹根
        dfs(a);
        printf("%d\n",max(dp[a][1],dp[a][0]));


    }
    return 0;
}

 

 

/*
HDU 1540
簡單樹形DP
結構體實現鏈表建樹

G++  109ms  416K
*/

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=6010;

struct Node
{
    int v;
    Node *next;
};
Node *head[MAXN];//頭指針
Node edge[MAXN*2];//這個要大一點
int tol;//邊的總數,也就是edge數組
int dp[MAXN][2];
int hap[MAXN];
bool vis[MAXN];
void init()
{
    tol=0;
    memset(dp,0,sizeof(dp));
    memset(head,NULL,sizeof(head));//初始化頭指針
    memset(vis,false,sizeof(vis));
}
void add_edge(int a,int b)//加一條a與b的無向邊
{
    edge[tol].v=b;
    edge[tol].next=head[a];
    head[a]=&edge[tol++];

    edge[tol].v=a;
    edge[tol].next=head[b];
    head[b]=&edge[tol++];
}
void dfs(int v)
{
    if(vis[v])return;
    vis[v]=true;
    Node *p=head[v];
    dp[v][1]=hap[v];
    while(p!=NULL)
    {
        if(!vis[p->v])
        {
            dfs(p->v);
            dp[v][0]+=max(dp[p->v][0],dp[p->v][1]);
            dp[v][1]+=dp[p->v][0];
        }
        p=p->next;
    }
}
int main()
{
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int n,a,b;
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(int i=1;i<=n;i++)
           scanf("%d",&hap[i]);
        while(scanf("%d%d",&a,&b))
        {
            if(a==0&&b==0)break;
            add_edge(a,b);
        }
        //由於建的是無向圖,可以任意找個點當樹根進行DP
        //但是在搜索中要判重,加個vis數組
        dfs(1);
        printf("%d\n",max(dp[1][0],dp[1][1]));
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM