poj1947 Rebuilding Roads

2023-11-07 20:48
文章标签 roads rebuilding poj1947

本文主要是介绍poj1947 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 way to get from any given barn to any other barn.
Thus, the farm transportation system can be represented as a tree.

Farmer John wants to know how much damage another earthquake could do.
He wants to know the minimum number of roads whose destruction would
isolate a subtree of exactly P (1 <= P <= N) barns from the rest of
the barns.

Input
* Line 1: Two integers, N and P

  • Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J’s parent in the tree of roads.

Output A single line containing the integer that is the minimum number
of roads that need to be destroyed for a subtree of P nodes to be
isolated.

树形dp,dp[i][j]表示i为根的子树留下j个节点的最小费用。
枚举儿子的时候要不断更新dp[i][j],dp[i][j]=min{dp[i][j]+1,dp[i][j-k]+dp[son][k]},前一种表示把子树切掉,后一种不切。初始条件是dp[i][1]=0。
因为答案不一定要留下根,所以要边dfs边更新答案。

这篇关于poj1947 Rebuilding Roads的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/366198

相关文章

hdu 1301 Jungle Roads (基础最小生成树)

题目:         链接:点击打开链接 题意:         对n个村庄之间的路进行修理, 然后是n-1行,每行的第一组数据时一个大写字母VIL和一个数K,Vil表示从这个村庄出发,K表示刚才的那个字母代表的村庄和其他村庄的路的数目,接下来在同一行是K组数据,每组是一个大写字母和一个数,大写字母表示和第一个村庄连接的村庄,数表示维修他们之间的路所需的费用。现在为了使维修费油最低,只需所

NYOJ 434 POJ 1251 Jungle Roads(最小生成树)

链接:click here 题意: 题目大意在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使每个岛屿都间接或直接与其他岛屿相同时所用的的最短时间(只有修完一个桥后才可修下一个桥)。简言之就是求最小生成树。 对于数据,数据输入的第一行n代表岛屿的个数,当为0是结束程序,接着n-1行开始时为这岛屿的编号,用大写字母表示,接着是一个整数m,表示与该岛屿连接的字典序

道路交通英语 English for Roads and Transportation

COLLECTED FROM THE INTERNET 每日雅思 100个交通规则专用英语 交通规则  traffic regulation 路标    guide post 里程碑   milestone 停车标志  mark car stop 红绿灯   traffic light 自动红绿灯 automatic traffic signal light 红灯    red light 绿灯

poj1724--ROADS(最短路变形)

题目链接:点击打开链接 题目大意:给出n个点,m条路径(有向),每条边有一个花费和一个长度,要求在给定的花费内求1到n的最短路径 用dis[i][j]表示从1到i点,花费为j的最短路径,跑spfa,求出最短路 #include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace

poj3411--Paid Roads(bfs+状压)

题目链接:点击打开链接 题目大意:有n个点,m条有向边,经过边需要一个花费,a b c p q代表 a到b的一条道路,如果经过这条边之前经过c点,那么需要p的花费,否则需要q的花费,问从1点到n点的最小花费。 方法1、每条边可能会经过多次,每个点也可以经过多次,这样就没有了边界不能直接进行dfs,因为要记录之前经过的边,所以使用状压,dis[i][j]:当前在第i个点,j表示经过了的点,这样就

POJ 2421 Constructing Roads (MST)

题中给了n*n的矩阵,值是i点到j点的建边的花费,其中已经建好了m条边,题中求还需要花费多少才能使该图连通 思路:Kruskal更好做(并查集)。初始化之后,将m条边依次加入并查集,只要能合并及时合并。 /************************************************ Author: fisty* Created Time: 2015/2/28 14:04

POJ 1251 Jungle Roads (MST)

输入的时候需要转化 /************************************************ Author: fisty* Created Time: 2015/2/28 12:27:44* File Name : A.cpp*********************************************** */#include <iostrea

HDU 1102 Constructing Roads

这道题的要求是任意两个地点必须直接相连或者只通过一个地方间接相连。 所以合并a,b的时候,不只是把a和b并在一起,具体见UNION函数。 然后就是Kruskal算法求最小生成树就行了。 int N,Q; //边 struct edge{int from,to;int value;void init(int from,int to,int value){this->from=fr

hnu 12947 Absurdistan Roads

hnu 12947  网址  http://acm.hnu.cn/online/?action=problem&type=show&id=12947 给出n    和 n*n的矩阵  a(i,j)表示i 到j 的距离   然后求n条边 是的这个最短路成立 用求最小生成树的方法求出n-1条边 这n-1条边肯定是有用的  主要是如何求出第n条边 用floyd求出任意两点之间的最短距离

UVa 10308 Roads in the North 树的直径

题目来源:UVa 10308 Roads in the North 题意:求距离最远的2点之间的距离 思路:裸的树的直径 或者树形DP #include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn = 100010;struct node{int to, w;node()