【题目链接】

点击打开链接

【算法】

f[i][j]表示以i为根的子树中,最少删多少条边可以组成j个节点的子树

树上背包,即可

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 200
const int INF = 1e8; int i,j,n,p,x,y,ans = INF;
int sum[MAXN],f[MAXN][MAXN];
vector<int> e[MAXN]; inline void dfs(int x)
{
int i,j,k,y;
sum[x] = ;
for (i = ; i < e[x].size(); i++)
{
y = e[x][i];
dfs(y);
sum[x] += sum[y];
for (j = sum[x]; j > ; j--)
{
for (k = ; k < j; k++)
{
f[x][j] = min(f[x][j],f[x][j-k]+f[y][k]-);
}
}
}
} int main()
{ scanf("%d%d",&n,&p);
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
if (j > ) f[i][j] = INF;
}
}
for (i = ; i < n; i++)
{
scanf("%d%d",&x,&y);
e[x].push_back(y);
f[x][]++;
}
dfs();
for (i = ; i <= n; i++)
{
if (i == ) ans = min(ans,f[i][p]);
else ans = min(ans,f[i][p]+);
}
printf("%d\n",ans); return ; }

【POJ 1947】 Rebuilding Roads的更多相关文章

  1. 【树形dp】Rebuilding Roads

    [POJ1947]Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11934   Accep ...

  2. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  3. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  4. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  5. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  6. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  7. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  8. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  9. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

随机推荐

  1. Cake(凸包+区间DP)

    You want to hold a party. Here's a polygon-shaped cake on the table. You'd like to cut the cake into ...

  2. 【概率dp】D. Card Collector

    https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/D [题意] 为了集齐n张卡片,必须要买多少袋零食?题目给定每种卡片出现在零食中的 ...

  3. phpstorm的破解

    按照PHPstorm进入如下页面: 然后继续单击License  server  输入:http://www.0-php.com:1017    PHPstorm完美运行!!!!!

  4. poj -1185 炮兵阵地 (经典状压dp)

    http://poj.org/problem?id=1185 参考博客:http://poj.org/problem?id=1185 大神博客已经讲的很清楚了,注意存状态的时候是从1开始的,所以初始化 ...

  5. HDU 1244 【DP】

    题意: 中文. 思路: 先初步处理,用give-take求出每个城市剩的钱. 求解问题转化成使得和不小于0的最长连续字串. 枚举起点,然后当该起点加的和为负时开始枚举下一起点.(这个状态的转移) 2W ...

  6. 【.Net Core 学习系列】-- 自定义错误页面在IE浏览器中不能正常显示

    测试场景: 1. 新建.Net Core Web项目 2. 选择模板: 3. 修改Error页面代码:(去掉母版页并修改页面显示信息) 4. 修改[ASPNETCORE_ENVIRONMENT],并抛 ...

  7. 多平台密码绕过及提权工具Kon-Boot的使用与防范

    在单用户的机器上密码可能没那么重要,但是一旦有多个用户可以使用这台机器,密码就显得十分必要了(比如有儿童账户的电脑).所以说为你的电脑增设一条防线的最常用,最简单的方法就是用密码将你的电脑保护起来,但 ...

  8. 基于51单片机的CAN通讯协议C语言程序

      //-----------------------函数声明,变量定义-------------------------------------------------------- #includ ...

  9. 配置Python 2.7.1外加环境pywin32-216.win32-py2.7

    python-2.7.1  安装包 下载地址:http://download.csdn.net/detail/baidu_14854543/7985187 pywin32-216.win32-py2. ...

  10. DacningLinks实现

    本文简单分析DancingLinks实现中的数据结构设计,给出了精确覆盖问题及其扩展问题的代码.并应用于数独问题. 先简单描写叙述一下精确覆盖问题: 给定一个N*M的01矩阵,从中选中若干行,这些行向 ...