标签:font iss arch nes with present rri names span
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11495 | Accepted: 5276 |
Description
Input
Output
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
Source
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N=155,INF=1e9; int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();} return x*f; } int n,m,u,v,w,ind[N]; struct edge{ int v,w,ne; }e[N<<1]; int h[N],cnt=0; void ins(int u,int v){ cnt++; e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt; } int d[N][N],size[N]; void dfs(int u){ int child=0;size[u]=1;//!self for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; dfs(v); size[u]+=size[v]; child++; } if(!child) {size[u]=1;d[u][1]=0;return;}//printf("size %d %d\n",u,size[u]); d[u][1]=child; for(int j=2;j<=size[u];j++) d[u][j]=INF; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; for(int j=size[u];j>=1;j--){ int t=min(j-1,size[v]); for(int k=1;k<=t;k++) d[u][j]=min(d[u][j],d[u][j-k]+d[v][k]-1); } } //for(int i=1;i<=size[u];i++) printf("d %d %d %d\n",u,i,d[u][i]); } int main(int argc, const char * argv[]) { n=read();m=read(); for(int i=1;i<=n-1;i++){ u=read();v=read();ins(u,v);ind[v]++; } int root=-1; for(int i=1;i<=n;i++) if(!ind[i]) {root=i;break;} dfs(root); int ans=INF; for(int i=1;i<=n;i++) if(size[i]>=m) ans=min(ans,d[i][m]+(i==root?0:1)); //,printf("ans %d %d\n",i,d[i][m]); printf("%d",ans); return 0; }
POJ1947 Rebuilding Roads[树形背包]
标签:font iss arch nes with present rri names span
原文地址:http://www.cnblogs.com/candy99/p/6072077.html