二叉苹果树

#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;
}

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

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