POJ1947 Rebuilding Roads
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11334 | Accepted: 5222 |
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.]
Source
给你一棵有n个节点的树,可以砍掉其中一些边,问最少砍几条边可以留下一个有p个节点的树(剩下的必须是一整棵树)
标准树形DP
各种改都改不对,无奈开启了代码标准比对模式,仍然没用。
这时我注意到不管怎么改输出的都是一样的解,md卡程序了!关了IDE,删了编译好的程序,开IDE重编译,输出了正解……
然而我恐怕再也无法知道我到底是在哪一步改对了的。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 const int mxn=160; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 13 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 14 return x*f; 15 } 16 int n,p; 17 int f[mxn][mxn]; 18 int num[mxn]; 19 vector<int>e[mxn]; 20 void dp(int u){ 21 int i,j,k; 22 num[u]=1; 23 if(!e[u].size()){f[u][1]=0;return;} 24 for(i=0;i<e[u].size();i++){ 25 int v=e[u][i]; 26 dp(v); 27 num[u]+=num[v]; 28 for(j=num[u];j;j--){ 29 for(k=1;k<j;k++){ 30 f[u][j]=min(f[u][j],f[u][j-k]+f[v][k]-1); 31 } 32 } 33 } 34 return; 35 } 36 int main(){ 37 n=read();p=read(); 38 int i,j; 39 int u,v; 40 for(i=1;i<n;i++){ 41 u=read();v=read(); 42 e[u].push_back(v); 43 num[u]++; 44 } 45 memset(f,0x3f,sizeof f); 46 for(i=1;i<=n;i++){ 47 f[i][1]=num[i]; 48 } 49 dp(1); 50 int ans=f[1][p]; 51 for(i=1;i<=n;i++){ 52 ans=min(ans,f[i][p]+1); 53 } 54 printf("%d\n",ans); 55 return 0; 56 }
本文为博主原创文章,转载请注明出处。