【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][ ...
随机推荐
- tarjan求割边割点
tarjan求割边割点 内容及代码来自http://m.blog.csdn.net/article/details?id=51984469 割边:在连通图中,删除了连通图的某条边后,图不再连通.这样的 ...
- 【模拟】2017 Multi-University Training Contest 1 The Battle of Chibi
acm.hdu.edu.cn/showproblem.php?pid=5542 [Accepted] #include<iostream> #include<cstdio> # ...
- 【随机化算法】codeforces Matrix God
http://codeforces.com/gym/101341 [题意] 给定三个方阵A,B,C,问AB=C是否成立? 方阵的规模最大为1000 [思路] 求AB的时间复杂度为n*n*n,会超时 左 ...
- 最小生成树求法 Prim + Kruskal
prim算法的思路 和dijkstra是一样的 每次选取一个最近的点 然后去向新的节点扩张 注意这里的扩张 不再是 以前求最短路时候的到新的节点的最短距离 而是因为要生成一棵树 所以是要连一根最短的连 ...
- ACM-ICPC 2018 焦作赛区网络预赛 H、L
https://nanti.jisuanke.com/t/31721 题意 n个位置 有几个限制相邻的三个怎么怎么样,直接从3开始 矩阵快速幂进行递推就可以了 #include <bits/st ...
- 简论远程通信(RPC,Webservice,RMI,JMS的区别)
RPC(Remote Procedure Call Protocol)RPC使用C/S方式,采用http协议,发送请求到服务器,等待服务器返回结果.这个请求包括一个参数集和一个文本集,通常形成“cla ...
- 转: java DES的算法片码
转自: https://www.zhihu.com/question/36767829 作者:郭无心链接:https://www.zhihu.com/question/36767829/answer/ ...
- 判断所ping主机的操作系统
根据它的值判断所ping主机的操作系统类型. TTL被称为生存期,也就是你所传输的数据在网络上经过的路由器的最大个数. 操作系统 TTLLINUX 64WIN2K/NT 128WINDOWS 系列 3 ...
- [外文理解] DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构。
原文:http://www.infoq.com/interviews/Technology-Influences-DDD# 要实现DDD(domain drive design 领域驱动设计)原始意 ...
- PHP swfupload图片上传实例
swfupload已经是第二次研究,这次自已整了个简单demo,无奈菜鸟最杯… PHP代码如下: if (isset($_FILES["Filedata"]) || !is_upl ...