zoukankan      html  css  js  c++  java
  • HDU

    先上题目:

    Anniversary party

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


    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
     
      题意:给你一棵树,树上每个节点都有一个权值,在这棵树上选一些点,其中这些点不能是相邻的,要是这些点的权值之和最大。
      这是一条树DP的例题→_→。
      分析:  
      首先对于任何的节点做根都不会对最终答案产生影响。
      其次每一个点有两种选择方案:选或者不选。
      再者选了一个节点以后与它相邻的节点都不可以选了。
      这是一题树DP,所以我们需要考虑一下DP的方向,在一棵树上DP,方向一般有两种:①将根的信息传到子节点上面②将子节点的信息传到根上。当然还有一种就是需要结合从根传来的信息和从子节点上传来的信息分析得出答案。
      这里我们考虑的思路是将子节点的我信息传到根上。定义dp[i][j]代表以第i个节点为根的子树能得到的最大值,j=0代表选i, j=1代表不选i,那么状态转移方程就是:
      dp[i][1]=Σmax(dp[k][0],dp[k][1])   k是i的子节点编号
      dp[i][0]=v[i] + Σdp[k][1]               v[i]是节点i的权值,k是i的子节点编号
      最终结果就是max{dp[root][0],dp[root][1]}
     
    上代码:
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #define max(x,y) (x > y ? x : y)
     4 #define MAX 6003
     5 
     6 using namespace std;
     7 
     8 int v[MAX];
     9 int p[MAX],tot;
    10 int c[MAX],to[MAX];
    11 int dp[2][MAX];
    12 bool f[MAX];
    13 
    14 
    15 void dfs(int r){
    16     for(int i=p[r];i!=-1;i=to[i]){
    17         dfs(c[i]);
    18         dp[1][r]+=dp[0][c[i]];
    19         dp[0][r]+=max(dp[0][c[i]],dp[1][c[i]]);
    20     }
    21 }
    22 
    23 void add(int l,int k){
    24     c[tot]=l;
    25     to[tot]=p[k];
    26     p[k]=tot++;
    27 }
    28 
    29 int main()
    30 {
    31     int n,l,k,maxn,r;
    32     //freopen("data.txt","r",stdin);
    33     while(scanf("%d",&n)!=EOF){
    34         memset(dp,0,sizeof(dp));
    35         for(int i=1;i<=n;i++){
    36             scanf("%d",&v[i]);
    37             dp[1][i]=v[i];
    38         }
    39         tot=0;
    40         memset(p,-1,sizeof(p));
    41         memset(c,0,sizeof(c));
    42         memset(to,0,sizeof(to));
    43         memset(f,0,sizeof(f));
    44         while(scanf("%d %d",&l,&k),(k+l)){
    45             add(l,k);
    46             f[l]=1;
    47         }
    48         for(int i=1;i<=n;i++){
    49             if(!f[i]){
    50                 r=i;
    51                 break;
    52             }
    53         }
    54         dfs(r);
    55         maxn=max(dp[0][r],dp[1][r]);
    56         printf("%d
    ",maxn);
    57     }
    58     return 0;
    59 }
    1520
     
    Sample Output
    5
     
  • 相关阅读:
    对互联网海量数据实时计算的理解 + 业界开源实时流处理系统小结 _ (技术调研参考)
    SQL语句的添加、删除、修改多种方法 —— 基本操作
    leetcode-验证二叉搜索树
    leetcode-汉明距离
    leetcode-帕斯卡三角形
    leetcode-位1的个数(位与运算)
    leetcode-打家劫舍(动态规划)
    leetcode-回文链表
    leetcode-反转链表
    leetcode-最大子序和(动态规划讲解)
  • 原文地址:https://www.cnblogs.com/sineatos/p/3555894.html
Copyright © 2011-2022 走看看