树形DP(Rebuilding Roads poj1947)】的更多相关文章

题意:给出一颗树,求要形成一颗元素个数是p的子树,最少要去掉多少边 #include"stdio.h" #include"string.h" #include"stdlib.h" #include"algorithm" #define M 222 #define inf 0x3f3f3f3f using namespace std; struct node { int u,v,next; }edge[M*2]; int t,h…
Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows d…
题目大概是给一棵树,问最少删几条边可以出现一个包含点数为p的连通块. 任何一个连通块都是某棵根属于连通块的子树的上面一部分,所以容易想到用树形DP解决: dp[u][k]表示以u为根的子树中,包含根的大小k的连通块最少的删边数 要求答案就是min(dp[u][p],min(dp[v][p]+1)),u是整棵树的根,v是其他结点 转移从若干个子树各自选择要提供几个k转移,不过指数级时间复杂度,当然又是树上背包了.. 转移好烦,写得我好累好累..还好1A了.. #include<cstdio> #…
题目大意 给定一棵n个结点的树,问最少需要删除多少条边使得某棵子树的结点个数为p 题解 很经典的树形DP~~~直接上方程吧 dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v][k]-1) 方程的意思是 以u结点为根保留j个结点需要删除的最少的边的条数,那么可以选择在某个子结点v中选择k个保留,其他结点保留j-k个,为什么需要-1呢,因为相当于把子树v衔接到结点u上,因此边u->v是不需要删除的,所以要-1 代码: #include <iostream> #inc…
[POJ1947]Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11934   Accepted: 5519 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake las…
Rebuilding Roads   Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one wa…
Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9105   Accepted: 4122 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The…
版权声明:本文为博主原创文章,未经博主允许不得转载. Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8227   Accepted: 3672 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrib…
题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.son) + 1,即剪掉与所有儿子(total(i.son))的边,还要剪掉与其父亲(+1)的边. dp[i][k] = min(dp[i][k],dp[i][j - k] + dp[i.son][k] - 2),即由i.son生成一个节点数为k的子树,再由i生成其他j-k个节点数的子树. 这里要还原i…
Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9957   Accepted: 4537 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The…