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;
}
#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;
}
阅读全文
0 0
- Rebuilding Roads
- PKU 1947 Rebuilding Roads
- POJ 1947 Rebuilding Roads
- poj 1947 Rebuilding Roads
- POJ1947 Rebuilding Roads
- POJ 1947 Rebuilding Roads
- POJ 1947 Rebuilding Roads
- POJ1947--Rebuilding Roads
- poj 1947 Rebuilding Roads
- POJ 1947 Rebuilding Roads
- POJ 1947 Rebuilding Roads
- poj-1947-Rebuilding Roads
- POJ 1947 Rebuilding Roads
- POJ 1947 Rebuilding Roads
- poj1947 Rebuilding Roads
- POJ 1947 Rebuilding Roads
- POJ 1947 Rebuilding Roads
- POJ 1947--Rebuilding Roads
- liunx sed 修改文件
- 新增的html5属性
- Python模块学习--shutil和hashlib和json
- php基础知识(2)
- pymongo统计字段是否存在的记录
- Rebuilding Roads
- 求重心的板子
- 堆栈
- MySQL 绿色版安装Window 系统
- javascript 实现数组或对象排序
- 用ExpandableListAdapter实现从网络获取数据展购物车
- PHP与软件破解导学
- 總結——關於2017 10 24測試的分析總結
- python 爬取携程网飞机航班信息