dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t)  < i=(1,n) , t = i是否为整棵树的根 > 更新的时候分为两种情况..一种是要从其这个孩子转移过来...枚举做01背包..更新出每个状态的最小值..或者说直接砍掉这个孩子..那么只需将所有的状态多加个砍边... 这里的枚举做01背包..意思是由于叶子节点要放多少进去不确定..叶子节点要放的大小以及本节点的空间都在枚举更新...这种概念就是泛化背包..本质上是0…
dp[x][y]代表以x为根的子树..连接了y个终端用户(叶子)..所能获得的最大收益... dp[x][ ]可以看成当根为x时..有个背包空间为0~m...每个空间上记录了到到达这个空间的最大收益.. 典型的泛化背包问题... Program: #include<iostream> #include<stdio.h> #include<string.h> #include<set> #include<ctime> #include<alg…
题目链接: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[…
题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j]-1,son是u的儿子节点.初始是将所有的儿子都断开,然后-1代表的是这个儿子我需要了,不断了. #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define…
题意:给一棵树,在树中删除一些边,使得有一个连通块刚好为p个节点,问最少需要删除多少条边? 思路: 因为任一条边都可能需要被删除,独立出来的具有p个节点的连通块可能在任意一处地方.先从根开始DFS,然后进行树DP,dp[t][i]表示在以t为根的子树中删除i个点需要删除多少条边.dp[t][n-p]有可能是答案了,但是这种仅考虑到从树上脱落掉部分子树,那么留下的连通块通常是与1号点(树根)相连的,那如果所需要的连通块是在某棵子树中呢?将所有可能的子树取出来,若该子树节点数>=p,那么就可以在该子…
Rebuilding Roads   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 wa…
Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9105   Accepted: 4122 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…
版权声明:本文为博主原创文章,未经博主允许不得转载. Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8227   Accepted: 3672 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrib…
Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9957   Accepted: 4537 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…
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…