zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    C#读取并修改app.congig的实例
    apache:添加cgi模式
    初识golang
    Golang: pprof
    Golang:测试map是否存在
    beego: 获取request参数
    shell:crontab
    初识Iaas,paas
    初识golang
    Go-new和make
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2659716.html
Copyright © 2011-2022 走看看