Rebuilding Roads
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9157 | Accepted: 4145 |
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
一棵树有N个节点,最少删掉多少条边使有一棵子树有P个节点。
设dp[i][j]为节点i作为根的树至少删掉多少条边有j个节点(删完之后的树还是以i为根)。dp[i][j]=min(dp[i][j],dp[i][j-k]+dp[v][k]-1),也就是在以v为根的子树中有k个节点,减1是因为一开始假设把和根连的所有子节点的那条边都删掉了,现在又加上这棵子树,所以这棵子树和根连的边要加回来。注意这里1<=k<j,因为子节点存在父节点必须存在,所以至少有一个节点i,以v为根的最多j-1个节点。dp[i][1]就是i连接的子节点的个数。最后答案找dp[i][P]最小的那个,当i不为1的时候要加1,和它的父节点切断。
通过这道题总结了一下,这种树形背包要注意边界情况,注意初始化,对于根节点要预先处理或者左后处理,一般根节点也属于一次背包。如果父节点对子节点有约束的话注意DP循环范围,子节点要在满足父节点条件的情况下进行DP。
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define INF 0x3f3f3f3f
#define MAXN 160
#define MAXM 100010
#define MAXNODE 100010*4
#define MOD 999983
typedef long long LL;
using namespace std;
int N,P,vis[MAXN],dp[MAXN][MAXN],cnt[MAXN],num[MAXN];
vector<int> G[MAXN];
void get_cnt(int u){
vis[u]=1;
int len=G[u].size();
for(int i=0;i<len;i++){
int v=G[u][i];
if(!vis[v]){
cnt[u]++;
get_cnt(v);
}
}
}
int DFS(int u){
vis[u]=1;
num[u]=1;
int len=G[u].size();
dp[u][1]=cnt[u];
for(int i=0;i<len;i++){
int v=G[u][i];
if(!vis[v]){
num[u]+=DFS(v);
for(int j=num[u];j>=1;j--)
for(int k=1;k<j&&k<=num[v];k++) dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v][k]-1);
}
}
return num[u];
}
int main(){
freopen("in.txt","r",stdin);
while(scanf("%d%d",&N,&P)!=EOF){
for(int i=1;i<=N;i++) G[i].clear();
int u,v;
for(int i=0;i<N-1;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
get_cnt(1);
memset(vis,0,sizeof(vis));
memset(dp,INF,sizeof(dp));
DFS(1);
int ans=dp[1][P];
for(int i=2;i<=N;i++) ans=min(ans,dp[i][P]+1);
printf("%d\n",ans);
}
return 0;
}