Rebuilding Roads

来源:互联网 发布:mac 图片转文字 编辑:程序博客网 时间:2024/06/06 22:35
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;


const int maxn=10000;


vector<int> tree[200];


int dp[200][200];
int n,p;
bool son[200];




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--)
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()
{
cin>>n>>p;
for (int i = 0; i < n - 1; i++)
{
int a,b;


cin>>a>>b;
tree[a].push_back(b);
son[b]=1;


}
int root = 1;
while(son[root])
root++;
for(int i=1; i<=n; i++)
{
dp[i][1]=tree[i].size()+1;
for (int j = 2; j <= p; j++)
dp[i][j] = maxn;
}


dfs(root);
dp[root][p]--;
int ans = maxn;
for (int i = 1; i <= n; i++)
ans = min(ans, dp[i][p]);
cout<<ans;
return 0;




}
原创粉丝点击