POJ 1947 树形DP

题目

Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 10679 Accepted: 4898
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

题意

有一个n节点的树,问最小要减去多少条边使得剩下p的节点

题解

dp[i][j]得到以i为根有j个节点树所需去掉的最小边数
有dp[i][j] = min(min(dp[i][k]+dp[t][j-k]),dp[i][j]+1)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#include <iomanip>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define m_1(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
#define lowbit(a) ((a)&(-a))
#define FFR freopen("data.in","r",stdin)
#define FFW freopen("data.out","w",stdout)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
const ld PI = acos(-1.0);

using namespace std;
#define SIZE (200)

int father[SIZE],brother[SIZE],son[SIZE];
int dp[SIZE][SIZE];
int n, p;

void dfs(int root) {
    int k = son[root];
    dp[root][1] = 0;
    while (k) {
        dfs(k);
        int i, j;
        fi(i, p, 1) {
            int tem = dp[root][i] + 1;
            f(j, 1, i - 1) {
                tem = min(tem, dp[root][i - j] + dp[k][j]);
            }
            dp[root][i] = tem;
        }
        k = brother[k];
    }
}

int main() {
    //ios_base::sync_with_stdio(false); cin.tie(0);
    while (~scanf("%d%d",&n,&p)) {
        int i;
        m0(son); m0(brother); m0(father);
        f(i, 1, n-1) {
            int x, y;
            scanf("%d%d", &x, &y);
            father[y] = x;
            brother[y] = son[x];
            son[x] = y;
        }

        int root;
        f(root, 1, n)
            if (father[root] == 0)
                break;
        mm(dp);
        dfs(root);
        int ans = dp[root][p];
        f(i, 2, n)
            ans = min(ans, dp[i][p] + 1);
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值