POJ 1947 Rebuilding Roads (树形dp 经典题)

Rebuilding Roads

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9499   Accepted: 4317

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

USACO 2002 February

题目链接:http://poj.org/problem?id=1947

题目大意:一颗含有n个结点的树,求减去最少的边使得该树只含有p个结点

题目分析:典型的树形dp,在树上进行动态规划,设dp[s][i]表示以s为根的子树含有i个结点需要删去的边数,则得到转移方程:

对于i的子树k:

1.不加子树k,dp[s][i] = dp[s][i] + 1 (不加子树k,相当于删去一条边)

2.加子树k,dp[s][i] = min(dp[s][j] + dp[k][i - j])  (1<= j <= i)   (以s为根的树的结点数加上其子树的结点数等于i)

最后答案就是在dp[i][m]中取小,要注意的一点是,如果i不是根,值还需要+1,因为要脱离原来的根,还要去掉一条边

这里需要注意,dp过程是自下而上的,也就是从叶子到根,而不是从根到叶子

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 155;

struct Edge
{
    int to, next;
}e[MAX * MAX / 2];

int n, m;
int head[MAX], cnt, root;
int fa[MAX], dp[MAX][MAX];

void Add(int x, int y)  //加边
{
    e[cnt].to = y;
    e[cnt].next = head[x];
    head[x] = cnt++;
}

void DFS(int u)
{
    for(int i = 0; i <= m; i++) //初始化为无穷大
        dp[u][i] = INF;
    dp[u][1] = 0;               //根结点本就一个点,不需要减边
    for(int i = head[u]; i != -1; i = e[i].next)  //同层
    {
        int v = e[i].to;    //u的子树
        DFS(v);
        for(int j = m; j >= 1; j--)
        {
            for(int k = 0; k < j; k++)
            {
                if(k)   //不加子树k
                    dp[u][j] = min(dp[u][j], dp[u][j - k] + dp[v][k]);
                else    //加上子树k
                    dp[u][j] = dp[u][j] + 1;
            }
        }
    }
}

int main()
{
    scanf("%d %d", &n, &m);
    cnt = 0;
    memset(fa, -1, sizeof(fa));
    memset(head, -1, sizeof(head));
    for(int i = 1; i < n; i++)
    {
        int x, y;
        scanf("%d %d", &x, &y);
        Add(x, y);
        fa[y] = x;
    }
    for(root = 1; fa[root] != -1; root = fa[root]);
    DFS(root);
    int ans = dp[root][m];
    for(int i = 1; i <= n; i++)     //除了根节点,其他节点要想成为独立的根,必先与父节点断绝关系,所以要先加1
        ans = min(ans, dp[i][m] + 1);
    printf("%d\n",ans);
}
时间: 2024-12-10 09:59:51

POJ 1947 Rebuilding Roads (树形dp 经典题)的相关文章

[POJ 1947]Rebuilding Roads (树形dp)

题目链接:http://poj.org/problem?id=1947 题目大意:给你一棵树,树上N个节点.问最少拆掉多少条边使得存在一个联通块,有P个节点. 树形dp,设计状态:dp[u][i]代表以u为根节点的剩下i个节点最少需要拆掉多少条边. 状态转移:dp[u][i+j] = min(dp[u][i+j],dp[u][i]+dp[v][j]-1); 其中v是u的儿子节点. 相当于在树上跑01背包,即每个儿子节点去掉剩下j个的但是要连上u-v边,或者不去掉剩下j个的. 代码: 1 impo

[poj 1947] Rebuilding Roads 树形DP

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

POJ 1947 Rebuilding Roads (树dp + 背包思想)

题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去的边数. 考虑两种情况,去掉孩子节点v与去不掉. (1)去掉孩子节点:dp[u][j] = dp[u][j] + 1 (2)不去掉孩子节点:dp[u][j] = min(dp[u][j - k] + dp[v][k]) 综上就是dp[u][j] = min(dp[u][j] + 1, min(dp[

POJ 2486 Apple Tree (树形dp 经典题)

Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7784   Accepted: 2603 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amoun

树形DP经典题

题目传送门 题意: 给出一棵树,求离每个节点最远的点的距离 思路: 把无根树转化成有根树分析, 对于上面那棵树,要求距结点2的最长距离,那么,就需要知道以2为顶点的子树(蓝色圈起的部分,我们叫它Tree(2)),距顶点2的最远距离L1 还有知道2的父节点1为根节点的树Tree(1)-Tree(2)部分(即红色圈起部分),距离结点1的最长距离+dist(1,2) = L2,那么最终距离结点2最远的距离就是max{L1,L2} f[i][0],表示顶点为i的子树的,距顶点i的最长距离 f[i][1]

POJ 1155 TELE 背包型树形DP 经典题

由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送数据到达另一个节点,电视台需要一定的费用 若可以传送数据到达用户的节点n-m+1~n,这些用户各自愿意支付一定的费用给电视台 现在电视台希望在不亏本的情况下为尽量多的用户转播比赛 输出最多可以为多少用户转播比赛 背包类型的树形DP第一题 dp[i][j]表示以节点i为根的子树有j个用户获得转播,电视

poj 1947 Rebuilding Roads 【树形DP】 【求至少删去树中 多少条边 使得树中节点数为P】

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10066   Accepted: 4595 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. Th

树形DP [POJ 1947] Rebuilding Roads

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

HDU 2196 Computer 树形DP 经典题

给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权,放在数组cost中 令tree(i)表示以节点i为根的子树 对于节点i,离该节点最远的点要不就是在tree(i)中,要不就是在father(i)上面 令: dp[i][1] : 在子树tree(i)中,离i最远的距离 dp[i][2] : 在子树tree(i)中,离i第二远的距离 (递推的时候需要)