首页 > 其他 > 详细

POJ1947---Rebuilding Roads(树形dp,背包)

时间:2015-05-13 10:36:49      阅读:178      评论:0      收藏:0      [点我收藏+]

Description
The cows have reconstructed Farmer John’s farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn’t have time to rebuild any extra roads, so now there is exactly one way to get from any given barn to any other barn. Thus, the farm transportation system can be represented as a tree.

Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.

Input
* Line 1: Two integers, N and P

  • Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J’s parent in the tree of roads.

Output
A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.

Sample Input

11 6
1 2
1 3
1 4
1 5
2 6
2 7
2 8
4 9
4 10
4 11

Sample Output

2

Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.]

Source

dp[u][size] 表示以u为根的子树,保留了size个节点时,最少需要删除的边个数
dp[u][size]=min(dp[u][size],dp[u][size?k]+dp[v][k]?1)

最后求个最小值,但是对于不是根的节点,答案要+1(与他的父亲节点脱离)

/*************************************************************************
    > File Name: POJ1947.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月12日 星期二 16时50分33秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int num[155];
int dp[155][155];
vector <int> tree[155];

void DP(int u) {
    num[u] = 1;
    int size = tree[u].size();
    for (int i = 0; i < size; ++i) {
        int v = tree[u][i];
        DP(v);
        num[u] += num[v];
    }
    dp[u][1] = size;
    for (int i = 0; i < size; ++i) {
        int v = tree[u][i];
        for (int j = num[u]; j > 0; --j) {
            for (int k = 1; k <= j && k <= num[v]; ++k) {
                if (dp[v][k] == inf || dp[u][j - k] == inf) {
                    continue;
                }
                dp[u][j] = min(dp[u][j], dp[v][k] + dp[u][j - k] - 1);
            }
        }
    }
}

int main() {
    int n, p;
    while (~scanf("%d%d", &n, &p)) {
        memset(dp, inf, sizeof(dp));
        for (int i = 1; i <= n; ++i) {
            tree[i].clear();
            num[i] = 0;
        }
        int u, v;
        for (int i = 1; i < n; ++i) {
            scanf("%d%d", &u, &v);
            tree[u].push_back(v);
        }
        DP(1);
        int ans = inf;
        for (int i = 1; i <= n; ++i) {
            if (num[i] < p) {
                continue;
            }
            if (i == 1) {
                ans = min(ans, dp[i][p]);
            }
            else {
                ans = min(ans, dp[i][p] + 1);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

POJ1947---Rebuilding Roads(树形dp,背包)

原文:http://blog.csdn.net/guard_mine/article/details/45675747

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!