codeforces1249F Maximum Weight Subset

Topic links: cf

Presented here is \ (O (n ^ 3) \) is the practice dp

Note \ (f_ {u, i} \) represents the (U \) \ subtree rooted at, and all selected points \ (U \) a distance of at least \ (I \) maximum point and the right

\ (i = 0 \) , the must is selected from the group \ (U \) , the rest is all its sons \ (V \) in \ (f_ {v, k} \)

\ (i> 0 \) , we enumerate \ (dis \) is the point where the smallest sub-tree \ (v \) , the enumeration sub-tree tree known contribution of the answer is \ (f_ {v, i- 1} \) , after consideration of the remainder of the subtree (referred to as root \ (W \) ), since this point will not be among the more than \) \ (V selected subtree in shallower point, they are to root \ (W \) a \ (DIS \) constant \ (\ GEQ-W. 1 \) , plus at least the distance between any two points \ (K +. 1 \) , so the sub-tree \ (W \) contributions \ (F_ {W, max (Ki,. 1-I)} \) . Finally, all \ (v \) to take the answer \ (max \) .

Also note that the above is that we need to find out \ (f \) seeking a suffix max, according to state this very clearly defined

The final answer is \ (f_ {1,0} \)

 #include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
const int N=10000;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define fir first
#define sec second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define maxd 998244353
#define eps 1e-8
struct node{int to,nxt;}sq[420];
int all=0,head[220];
int n,lim,a[220],f[220][220];

int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

void add(int u,int v)
{
    all++;sq[all].to=v;sq[all].nxt=head[u];head[u]=all;
}

void dfs(int u,int fu)
{
    for (int i=head[u];i;i=sq[i].nxt)
    {
        int v=sq[i].to;
        if (v==fu) continue;
        dfs(v,u);
    }
    rep(j,0,lim)
    {
        if (j==0)
        {
            f[u][0]=a[u];
            for (int i=head[u];i;i=sq[i].nxt)
            {
                int v=sq[i].to;
                if (v!=fu) f[u][0]+=f[v][lim];
            }
        }
        else
        {
            for (int i=head[u];i;i=sq[i].nxt)
            {
                int v=sq[i].to,now=f[v][j-1];
                if (v==fu) continue;
                for (int k=head[u];k;k=sq[k].nxt)
                {
                    int w=sq[k].to;
                    if ((v==w) || (fu==w)) continue;
                    now+=f[w][max(lim-j,j-1)];
                }
                f[u][j]=max(f[u][j],now);
            }
        }
    }
    per(i,lim,0) f[u][i]=max(f[u][i],f[u][i+1]);
}

int main()
{
    n=read();lim=read();
    rep(i,1,n) a[i]=read();
    rep(i,1,n-1)
    {
        int u=read(),v=read();
        add(u,v);add(v,u);
    }
    dfs(1,0);
    printf("%d",f[1][0]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/encodetalker/p/11729990.html