令$f[p][i]$表示以$p$为根的子树内,选了$i$个黑点,剩下的都是白点的这个子树内贡献的答案

如果$p$的子树都算出来了,只要计算$p$与$fa[p]$之间的边对答案的贡献就好了,贡献是$dis * (i * (sz - i) + (k - i) * (n - k - (sz - i)))$

于是树形动规一下就好了

 /**************************************************************
Problem: 4033
User: rausen
Language: C++
Result: Accepted
Time:308 ms
Memory:32300 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll; const int N = 2e3 + ; struct edge {
int next, to, v;
edge(int _n = , int _t = , int _v = ) : next(_n), to(_t), v(_v) {}
} e[N << ]; int n, k;
int first[N], tot;
int sz[N];
ll f[N][N], tmp[N]; inline void Add_Edges(int x, int y, int z) {
e[++tot] = edge(first[x], y, z), first[x] = tot;
e[++tot] = edge(first[y], x, z), first[y] = tot;
} inline ll calc(int x, int y) {
return 1ll * y * (x - y);
} #define y e[x].to
inline void dfs(int p, int fa, int w) {
int x, i, j;
sz[p] = ;
for (x = first[p]; x; x = e[x].next)
if (y != fa) {
dfs(y, p, e[x].v);
memcpy(tmp, f[p], sizeof(tmp));
for (i = min(sz[p], k); ~i; --i)
for (j = min(sz[y], k - i); ~j; --j)
tmp[i + j] = max(tmp[i + j], f[p][i] + f[y][j]);
memcpy(f[p], tmp, sizeof(tmp));
sz[p] += sz[y];
}
for (i = min(sz[p], k); ~i; --i)
f[p][i] += (calc(k, i) + calc(n - k, sz[p] - i)) * w;
}
#undef y int main() {
int i, x, y, z;
scanf("%d%d", &n, &k);
for (i = ; i < n; ++i) {
scanf("%d%d%d", &x, &y, &z);
Add_Edges(x, y, z);
}
dfs(, , );
printf("%lld\n", f[][k]);
return ;
}

最新文章

  1. codeforces 734E(DFS,树的直径(最长路))
  2. 【我是老中医】VMware在win8.1下开Ubuntu提示”内部错误&quot;解决方案
  3. 内网DMZ外网之间的访问规则
  4. Eclipse正在使用Ant扑灭Android数据包错误的解决方案 – Perhaps JAVA_HOME does not point to the JDK
  5. 优化EF性能
  6. HTML标签 按功能排序
  7. 极致21点开发DAY1
  8. TCP/IP协议--TCP的交互数据流和成块数据流
  9. oracle中REGEXP_SUBSTR方法的使用
  10. UDP,TCP的套接字编程的Python实现
  11. hadoop日志数据分析开发步骤及代码
  12. UITextView: 响应键盘的 return 事件
  13. yii框架场景的用法
  14. Linq in条件查询
  15. 【ZOJ3316】Game(带花树)
  16. environmentmap in unity
  17. MySQL协议分析(1)
  18. 机器学习 delay learning
  19. c# html 导出word
  20. 迪米特法則 Law of Demeter

热门文章

  1. Sublime Text 2 快捷键用法大全(转)
  2. XAF实现运行时填加验证规则并保存到数据库中
  3. [转]--android studio 使用gradle 导出jar包,并打包assets目录
  4. SQL中char、varchar、nvarchar的区别(zhuan)
  5. 关于JAVA中URL传递中文参数,取值是乱码的解决办法
  6. c point
  7. python成长之路【第一篇】:python简介和入门
  8. (四)C语言柔性数组、指针赋值
  9. Scala的Actor模式 &amp; Akka框架
  10. 本地获取System权限CMD方法汇总(转)