uva 11782 - Optimal Cut (树形dp)

  Optimal Cut 

cut of a binary tree is a set of tree nodes such as for each possible path from the root node to any leaf node, just one node in the cut belongs to the path. In a weighted binary tree, one can compute a weightedcut, because it would include weighted nodes. The total weight of such cut can be computed by adding up the weights of the nodes in it. The figure below shows a weighted binary tree. The gray nodes represent a weighted cut, and its total weight is 14.

Now, given a weighted binary tree, you have to write a program that finds the weighted cut with the maximal total weight value among all possible weighted cuts in the tree. This is hereafter called the Optimal Cut of the input binary tree. However, to make it a bit more interesting, your program must find the optimal cut that includes no more than K nodes, and report its weight. In the figure above, for instance, the nodes of the optimal cut sums 28 when K = 3, and 15 when K = 2.

Input 

The input can contain several problems. Each problem contains the description of a complete binary tree in a single line. This description corresponds to sequence of integer numbers, separated of each other by a blank space. The description starts with an integer  0H < 20  representing the height of the tree, followed by another integer  1K20  representing the maximum number of nodes in the optimal cut to be found. Then, the weights  -103Wi103  of the nodes follow, listed in pre-order. The end of input is indicated by a single line containing only an integer value of ` -1 '.

Output 

For each problem in the input, the output should be a single line with a single integer number, representing the total weight of the optimal cut found.

Sample Input 

2 3 8 6 7 -2 -1 2 1
0 1 1
3 3 -8 1 0 0 1 2 1 1 -1 1 1 1 3 1 4
-1

Sample Output 

9
1
5

第一次写树形dp,还是贴一发代码吧。

//11782
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = (1<<20) + 5;
const int maxk = 20 + 5;
const int INF = 2000000000;

int val[maxn];
int n;

void read(int x, int d){
    if(d > n) return;
    scanf("%d", &val[x]);
    read(2*x+1, d+1);
    read(2*x+2, d+1);
}

int dp[maxn][maxk];

int dfs(int x, int k, int d){
    if(d > n) return -INF;
    if(dp[x][k] != -INF) return dp[x][k];
    if(k == 1){
        return dp[x][k] = val[x];
    }

    dp[x][k] = dfs(x, k-1, d);
    for(int j = 1;j < k;j++){
        int teml = dfs(2*x+1, j, d+1);
        int temr = dfs(2*x+2, k-j, d+1);
        if(teml != -INF && temr != -INF)
            dp[x][k] = max(dp[x][k], teml+temr);
    }
    return dp[x][k];
}

int main(){
    int k;
    while(scanf("%d", &n)){
        if(n == -1) break;
        scanf("%d", &k);
        read(0, 0);

        for(int i = 0;i < (1<<(n+1));i++){
            for(int j = 0;j < maxk;j++){
                dp[i][j] = -INF;
            }
        }
        int ans = dfs(0, k, 0);
        printf("%d\n", ans);

    }
    return 0;
}


你可能感兴趣的:(uva 11782 - Optimal Cut (树形dp))