Logu P2015 Binary Apple Tree

Posted by HeinekenBeer on Fri, 28 Feb 2020 17:39:06 +0100

Catalog:

Analysis:

First, the last dynamic transfer equation:
f[a][j]=max(f[a][j],f[a][j−k−1]+f[to][k]+e[i].w)(1≤j≤min(m,king[a]),0≤k≤min(king[to],j−1))f[a][j]=max(f[a][j],f[a][j−k−1]+f[to][k]+e[i].w)(1≤j≤min(m,king[a]),0≤k≤min(king[to],j−1))
For this equation, the content is very understandable, king represents a tree rooted at point i has several edges, a represents the current node, and to represents a child node of A. Now you can think about it and understand it.
For scope, however, the small edition explains the following:
J:j stands for the number of branches reserved, because we have restricted the maximum number of branches m when entering, and tree a has only king[a] edges in total, so J must be less than or equal to king[a]
K:k stands for how many branches we cut in the subtree to, so it must be less than or equal to king[to], and as for j-1, we have to keep an edge that connects to a

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
inline LL read() {
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
    return d*f;
}
int n=read(),m=read();
int king[101],f[101][101],ls[101],x[101],c=0,w[101];
struct edg{
    int to,next,w;
}e[201];
void add(int a,int b,int www)
{
    e[c].to=b;e[c].w=www;e[c].next=ls[a];ls[a]=c++;
}
void dp(int a,int b)
{
    for(int i=ls[a];~i;i=e[i].next)
    {
        int to=e[i].to;
        if(to==b) continue;
        dp(to,a);
        king[a]+=king[to]+1;
        for(int j=min(m,king[a]);j>=1;--j)
          for(int k=min(king[to],j-1);k>=0;--k)
            f[a][j]=max(f[a][j],f[a][j-k-1]+f[to][k]+e[i].w);
    }
    return;
}
int main()
{     
    memset(ls,-1,sizeof ls);      
    int a,b,xw;
    for(int i=1;i<n;i++)
    {
        a=read();b=read();xw=read();
        add(a,b,xw);add(b,a,xw);
    }
    dp(1,0);
    printf("%d",f[1][m]);
    return 0;
}

Topics: less