码迷,mamicode.com
首页 > 其他好文 > 详细

树形dp

时间:2017-09-19 03:57:26      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:ural   之间   guest   state   wan   cal   lines   ica   理解   

zoj 2342

Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9480   Accepted: 5454

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

思路分析 :
  给出 n 个人, 并且他们之间有一定的上下级关系, 在纸上画一画 会发现她更像一棵树, 但是写此类 dp 就需要你对递归有一个比较好的理解, 模拟建立一棵树, 找到树的根节点,采用 dfs 遍历一遍树,并且将走过的点做一个标记

状态转移方程 :
  dp[node][1] += dp[i][0];
  dp[node][0] += max(dp[i][0], dp[i][1]);

代码示例:

  
/*
 * Author:  ry 
 * Created Time:  2017/9/18 18:16:33
 * File Name: 1.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int mm = 1e6+5;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long

int n;
int dp[6500][2];
int fa[6500];
bool vis[6500];

void dfs(int node){
    vis[node] = true;
    for(int i = 1; i <= n; i++){
        if (fa[i] == node && !vis[i]){
            dfs(i);
            dp[node][1] += dp[i][0];
            dp[node][0] += max(dp[i][0], dp[i][1]); 
        }
    }
}

int main() {
    int l, k;
    
    while(~scanf("%d", &n)){
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++){
            scanf("%d", &dp[i][1]);
            dp[i][0] = 0;
        }
        int roof;
        for(int i = 1; i <= n; i++){
            scanf("%d%d", &l, &k);
            if (l+k == 0) break;
            fa[l] = k;
            roof = k;        
        }
        while(roof){
            roof = fa[roof];
        }
        dfs(roof);
        printf("%d\n", max(dp[roof][0], dp[roof][1]));
    }

    return 0;
}

 

树形dp

标签:ural   之间   guest   state   wan   cal   lines   ica   理解   

原文地址:http://www.cnblogs.com/ccut-ry/p/7546159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!