题意:
有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树?
思路:
设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数。
dp[i][1] = total(i.son) + 1,即剪掉与所有儿子(total(i.son))的边,还要剪掉与其父亲(+1)的边。
dp[i][k] = min(dp[i][k],dp[i][j - k] + dp[i.son][k] - 2),即由i.son生成一个节点数为k的子树,再由i生成其他j-k个节点数的子树。
这里要还原i与i.son和其父亲的边所以-2。
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
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include <ctype.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
typedef long long ll;
const int maxn=6005;
const int INF=0x3f3f3f3f;
int n,p,root;
int dp[155][155];
int father[155],son[155],brother[155];
void dfs(int root)
{
int i,j,k,tem;
for(i = 0; i<=p; i++)
dp[root][i] = 10000000;
dp[root][1] = 0;
k = son[root];
while(k)
{
dfs(k);
for(i = p; i>=1; i--)
{
tem = dp[ro