有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树?
注意理解:删除某边,该边顶点的子树可以不管
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
using namespace std;
const int MAX = 10000;
vector <int> tree[160];
//设dp[i][k]为以i为根,生成节点数为k的子树 所需 剪掉 的 边数
int a, b, n, p, dp[160][160];
bool son[160];
void dfs(int root)
{
int len=tree[root].size();
for (int i = 0; i < len; i++)
{
dfs(tree[root][i]);//递归调用孩子结点(后根遍历)
for (int j = p; j > 1; j--)//j==1 的情况已经存在 >1 即可
for (int k = 1; k < j; k++)
dp[root][j] = min(dp[root][j], dp[root][j - k] + dp[tree[root][i]][k] - 2);
}
}
int main()
{
scan