Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
代码代码~总算过了#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int n,p; int head[160],to[310],nxt[310],cnt[160],vc; int bfso[160],fa[160],bg[160]; int dp[160][160],ans=0x3f3f3f3f; void bfs() { int h=0,t=1,q[1000];q[1]=1; while(h<t) { h++; bfso[h]=q[h]; for(int i=head[q[h]];i;i=nxt[i]) { if(to[i]!=fa[q[h]]) { fa[to[i]]=q[h]; t++; q[t]=to[i]; } } } } void init() { scanf("%d%d",&n,&p); memset(dp,0x3f,sizeof(dp)); int f,t; for(int i=1;i<=n-1;i++) { scanf("%d%d",&f,&t); to[++vc]=t; nxt[vc]=head[f]; head[f]=vc; to[++vc]=f; nxt[vc]=head[t]; head[t]=vc; } bfs(); for(int i=n;i>=1;i--) { cnt[bfso[i]]=1; for(int j=head[bfso[i]];j;j=nxt[j])if(to[j]!=fa[bfso[i]])cnt[bfso[i]]+=cnt[to[j]]; } } int main() { init(); memset(dp,0x3f3f3f3f,sizeof(dp)); for(int no=n;no>=1;no--) { int i=bfso[no]; dp[i][0]=0; for(int j=head[i];j;j=nxt[j]) if(fa[i]!=to[j]) { for(int v=cnt[i]-1;v>=0;v--) { int t=0x3f3f3f3f; for(int s=cnt[to[j]];s>=0;s--) if(v>=s)t=min(t,dp[i][v-s]+dp[to[j]][s]); dp[i][v]=t; } } for(int v=cnt[i];v>0;v--)dp[i][v]=dp[i][v-1]; dp[i][0]=1;dp[i][cnt[i]]=0; } ans=dp[1][p]; for(int i=2;i<=n;i++)ans=min(ans,dp[i][p]+1); printf("%d",ans); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator