【POJ 1947】 Rebuilding Roads
【题目链接】
【算法】
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的更多相关文章
- 【树形dp】Rebuilding Roads
[POJ1947]Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11934 Accep ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- [luoguP2016] 战略游戏(DP)
传送门 f[i][0]表示不选当前节点,当前节点的所有儿子节点都选f[i][1]表示选当前节点,儿子节点可选可不选 #include <cstdio> #include <cstri ...
- C#的特性学习草稿
原文发布时间为:2008-11-22 -- 来源于本人的百度文章 [由搬家工具导入] 举个简单的例子: 先定义个特性 从Attribute继承,并标明用法 [AttributeUsage(Attrib ...
- No route info of this topic
使用rocketmq时报错 com.alibaba.rocketmq.client.exception.MQClientException: No route info of this topic, ...
- XCode warning:“View Controller” is unreachable because it has no entry points
Unsupported Configuration: “View Controller” is unreachable because it has no entry points, and no i ...
- .net core webapi jwt 更为清爽的认证 ,续期很简单(2)
.net core webapi jwt 更为清爽的认证 后续:续期以及设置Token过期 续期: 续期的操作是在中间件中进行的,续期本身包括了前一个Token的过期加发放新的Token,所以在说续 ...
- Unique Paths II (dp题)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [Bzoj1499][NOI2005]瑰丽华尔兹[简单DP]
1499: [NOI2005]瑰丽华尔兹 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 1714 Solved: 1042[Submit][Status ...
- 使用Spring Data Redis操作Redis(单机版)
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...
- java获取本机机器名
java获取本机机器名 InetAddress.getLocalHost().getHostName().toString();
- eclipse默认指定项目的编译器版本
eclipse 提示 @Override must override a superclass method 问题解决 今天新换了一个Eclipse 版本: Build id: 20140224-06 ...