码迷,mamicode.com
首页 > 移动开发 > 详细

二叉苹果树

时间:2020-07-28 16:55:50      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:c++   ++   +=   void   for   min   pac   ace   style   

#include<bits/stdc++.h>
using namespace std;
const int N=450;
struct data {
    int to,stb,vol;
} a[N];
int head[2*N],root,tot,v[N],f[N][101],n,p,o[N];
int num[N];
void insert(int x,int y,int z) {
    a[++tot].stb=head[x];
    a[tot].to=y,a[tot].vol=z;
    head[x]=tot;
}
void dp(int x,int fa) {
    for(int i=head[x]; i; i=a[i].stb) {
        int xx=a[i].to;
        if(xx==fa) continue;
        dp(xx,x);num[x]+=num[xx]+1;
        for(int j=min(num[x],p); j; j--) {//j--
            for(int k=min(num[xx],j-1); k>=0; k--)
                f[x][j]=max(f[x][j],f[x][j-k-1]+f[xx][k]+a[i].vol);
        }
    }
//    for(int j=1; j<=p; j++)
//        cout<<f[x][j]<<" "<<x<<" "<<j<<endl;

}


int main() {
    scanf("%d%d",&n,&p);
    for(int i=1; i<=n-1; i++) {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        insert(x,y,z);
        insert(y,x,z);
    }
    dp(1,0);
    cout<<f[1][p];
    return 0;
}

改了好久,结果是数组开小了

二叉苹果树

标签:c++   ++   +=   void   for   min   pac   ace   style   

原文地址:https://www.cnblogs.com/cwjr/p/13384726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!