POJ 1947 Rebuilding Roads(基礎的樹形dp)
樹形dp,dp[i][j]中i表示節點i,j表示這個節點下面有多少點。dp表示此時最少刪掉了多少邊。如果子節點刪除那麼dp[i][j]+1,不刪除就不用管。dp[i][j] = min(dp[i][j]+1, min(dp[i][k]+dp[t][j-k])).t表示i的子節點。
Rebuilding Roads
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 8729 | Accepted: 3935 |
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.
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.
* 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.
Sample Input
11 6 1 2 1 3 1 4 1 5 2 6 2 7 2 8 4 9 4 10 4 11
Sample Output
2
Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn =200;
using namespace std;
int dp[maxn][maxn];
int num[maxn];
vector<int>g[maxn];
int n, m;
void dfs(int x)
{
for(int i = 0; i <= m; i++)
dp[x][i] = INF;
dp[x][1] = 0;
for(int i = 0; i < g[x].size(); i++)
dfs(g[x][i]);
if(g[x].size() == 0)
return;
for(int i = 0; i < g[x].size(); i++)
{
for(int j = m; j >= 0; j--)
{
int Min = INF;
for(int k = 0; k < j; k++)
Min = min(Min, dp[x][k] + dp[g[x][i]][j-k]);
dp[x][j] = min(dp[x][j]+1, Min);
}
}
}
int main()
{
while(~scanf("%d",&n))
{
scanf("%d",&m);
for(int i = 0; i <= n; i++)
g[i].clear();
int x, y;
for(int i = 0; i < n-1; i++)
{
cin >>x>>y;
g[x].push_back(y);
}
dfs(1);
int ans = dp[1][m];
for(int i = 2; i <= n; i++)
ans = min(ans, dp[i][m]+1);
cout<<ans<<endl;
}
return 0;
}
相關文章
- 【樹形dp】poj 1947 Rebuilding RoadsRebuild
- POJ 2486 Apple Tree(樹形dp)APP
- HDU 5326 Work (基礎樹形dp)
- POJ 3107 Godfather(樹形dp)Go
- POJ3107Godfather[樹形DP 樹的重心]Go
- POJ 1664 放蘋果 (基礎組合dp)蘋果
- [筆記]樹形dp筆記
- 樹形DP二三知識
- 「暑期訓練」「基礎DP」 Common Subsequence (POJ-1458)
- CCF之網路延時(樹形dp)
- hdu 4123 樹形DP+RMQMQ
- POJ 1724 ROADS(優先佇列+spfa)佇列
- 【動態規劃】樹形DP完全詳解!動態規劃
- POJ 3411 Paid Roads(搜尋的小技巧)AI
- ZROJ#398. 【18提高7】隨機遊走(期望dp 樹形dp)隨機
- poj2486Apple Tree[樹形揹包!!!]APP
- Linux基礎命令---顯示樹形程式pstreeLinux
- Puzzles CodeForces 696B 樹形DP 期望計算
- hdu4313 貪心並查集 || 樹形dp並查集
- Codevs1378選課[樹形DP|兩種做法(多叉轉二叉|樹形DP+分組揹包)---(▼皿▼#)----^___^]dev
- POJ1745Divisibility(dp)
- POJ2955 Brackets (區間DP)Racket
- POJ1849Two[DP|樹的直徑](擴充套件HDU4003待辦)套件
- Codeforces 461B. Appleman and Tree[樹形DP 方案數]APP
- 樹形dp - Codeforces Round #322 (Div. 2) F Zublicanes and Mumocrates
- SVG 基礎圖形SVG
- 【基礎dp】HDU 1260 Tickets
- poj 1180 dp的斜率優化優化
- POJ 1925 Spiderman(線性dp)IDE
- POJ 3744 概率dp+矩陣矩陣
- 關於一些基礎的dp——硬幣的那些事(dp的基本引入)
- CF 1029E Tree with Small Distances 樹形DP or 貪心
- BZOJ1864[ZJOI2006]三色二叉樹[樹形DP]二叉樹
- POJ1160 Post Office[序列DP]
- 【dp】POJ 1015 Jury CompromisePromise
- POJ 3249-Test for Job(拓撲排序&&DP)排序
- POJ 3017 單調佇列dp佇列
- POJ 3691 DNA repair (AC自動機 + dp)AI