传送门

设 $f[x][i]$ 表示 $x$ 的子树中,离 $x$ 最近的选择的节点距离为 $i$ 的合法方案的最大价值

设 $val[x]$ 表示节点 $x$ 的价值,首先有 $f[x][0]=val[x]$

那么考虑子树的合并,有 $f[x][min(i,j+1)]=max(f[x][min(i,j+1)],f[x][i]+f[v][j])$

注意此时 $f[x][i]$ 不能包括 $v$ 的贡献,这个可以搞个 $tmp$ 存一下新的 $f[x]$,最后统一覆盖掉即可

然后答案就是 $f[rt]$ 里面的状态取个最大值

复杂度 $n^3$

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=;
int n,m,val[N];
int fir[N],from[N<<],to[N<<],cntt;
inline void add(int a,int b) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; }
int f[N][N],tmp[N],ans;
vector <int> son[N];
void dfs(int x,int fa)
{
f[x][]=val[x];
for(int i=fir[x];i;i=from[i])
{
int &v=to[i]; if(v==fa) continue;
dfs(v,x); memset(tmp,,sizeof(tmp));
for(int j=;j<=n;j++)
for(int k=;k<=n;k++)
if(j+k+>m) tmp[min(j,k+)]=max(tmp[min(j,k+)],f[x][j]+f[v][k]);
for(int j=;j<=n;j++) f[x][j]=tmp[j];
}
}
int main()
{
n=read(),m=read(); int a,b;
for(int i=;i<=n;i++) val[i]=read();
if(n==) { printf("%d\n",val[]); return ; }
for(int i=;i<n;i++)
a=read(),b=read(),
add(a,b),add(b,a);
dfs(,);
for(int i=;i<=n;i++) ans=max(ans,f[][i]);
printf("%d\n",ans);
return ;
}

Codeforces 1249 F. Maximum Weight Subset的更多相关文章

  1. F. Maximum Weight Subset(贪心or树形dp解法)

    题:https://codeforces.com/contest/1249/problem/F 题意:给一颗树,边权为1,节点有点权,问取到一个点集,俩俩之间路径超过k,是点权和最大 思路:贪心地取点 ...

  2. CF1249F Maximum Weight Subset

    CF1249F Maximum Weight Subset 洛谷评测传送门 题目描述 You are given a tree, which consists of nn vertices. Reca ...

  3. codeforces#1249F. Maximum Weight Subset(树上dp)

    题目链接: http://codeforces.com/contest/1249/problem/F 题意: 一棵树的每个节点有个权值,选择一个节点集,使得任意点对的距离大于$k$ 求最大节点集权值, ...

  4. Codeforces 1249F Maximum Weight Subset (贪心)

    题意 在一颗有点权的树上,选若干个点,使得这些点两两距离大于k,且点权和最大 思路 贪心的取比较大的值即可 将所有点按照深度从大到小排序,如果当前点点权\(a[i]\)大于0,则将距离为k以内的所有点 ...

  5. 【CF1249F】Maximum Weight Subset(贪心)

    题意:给定一棵n个点带点权的树,要求从中选出一个点集,使得这些点两两之间距离都大于K,求最大点权和 n,K<=2e2,1<=a[i]<=1e5 思路:树形DP显然可做,极限是n方,然 ...

  6. Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle

    F. Maximum Balanced Circle 题目链接 题意 给出\(n\)个数,现在要从中选出最多的数\(b_i,b_{i+1},\cdots,b_k\),将这些数连成一个环,要求两两相邻的 ...

  7. Codeforces 828F Best Edge Weight - 随机堆 - 树差分 - Kruskal - 倍增算法

    You are given a connected weighted graph with n vertices and m edges. The graph doesn't contain loop ...

  8. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  9. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

随机推荐

  1. bind--dns-docker---[nslookup/dig]

    [dig]  https://www.cnblogs.com/apexchu/p/6790241.html [dns resolution and revserse ]https://www.cnbl ...

  2. oracle查询表指定字段类型

    查询表某字段类型,如下: SELECT data_type FROM all_tab_cols WHERE table_name = UPPER('SRIS_P_BaseInfo') and colu ...

  3. Laravel 中 Session 的使用问题(dd()导致laravel中session取值问题)

    Laravel 中 Session 的使用问题(dd()导致laravel中session取值问题) 一.总结 一句话总结: Laravel 会首先收集需要写入 Session 的所有数据,并在用户的 ...

  4. Python数据预处理(sklearn.preprocessing)—归一化(MinMaxScaler),标准化(StandardScaler),正则化(Normalizer, normalize)

      关于数据预处理的几个概念 归一化 (Normalization): 属性缩放到一个指定的最大和最小值(通常是1-0)之间,这可以通过preprocessing.MinMaxScaler类实现. 常 ...

  5. docker - nginx+php+php-mysql(扩展)

    Docker 安装 Nginx(https://www.runoob.com/docker/docker-install-nginx.html) Docker 安装 PHP(https://www.r ...

  6. python @classmethod

    写在前面 写博客的时候,我发现拖延症很严重,本来昨天要开始写的,结果东看看,西翻翻,啥也没落实下来.时间过去了,口袋里的收获却寥寥无几.讨厌这样的自己.我要戒掉这个不好的毛病. 拖延症的底层原因之一是 ...

  7. Javascript——概述 && 继承 && 复用 && 私有成员 && 构造函数

    原文链接:A re-introduction to JavaScript (JS tutorial) Why a re-introduction? Because JavaScript is noto ...

  8. 处理线上CPU负载过高的故障现象

    如何处理线上CPU100%的故障现象 处理流程: 1.登陆线上机器top命令,查看耗费cpu的进程号,举例来说发现进程24008持续耗费资源 2.top -Hp 24008去查看持续耗费cpu的线程号 ...

  9. Mysql主从复制(重置版)

    MySQL Replication是Mysql自带的一种功能,可以实现将数据从一台数据库服务器(master)复制到一台或多台数据库服务器(slave),默认情况下属于异步复制,无需维持长连接.通过配 ...

  10. 123457123456#0#-----com.twoapp.KidsShiZi01--前拼后广--儿童宝宝识字jiemei

    com.twoapp.KidsShiZi01--前拼后广--儿童宝宝识字jiemei