比赛 |
2024暑假C班集训D |
评测结果 |
AAAAAAAAAA |
题目名称 |
树上染色 |
最终得分 |
100 |
用户昵称 |
┭┮﹏┭┮ |
运行时间 |
0.542 s |
代码语言 |
C++ |
内存使用 |
34.43 MiB |
提交时间 |
2024-07-13 11:50:04 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define in inline
#define re register
const int N = 2e3+10;
const ll mod = 998244353;
in ll read(){
ll x = 0,f = 1;char c = getchar();
for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
return x * f;
}
int n,K;
struct edge{
int ver,nx;ll ed;
}e[N<<1];
int hd[N],tot;
in void link(int x,int y,ll z){e[++tot] = {y,hd[x],z};hd[x] = tot;}
int siz[N],fa[N];
ll f[N][N];
in void dp(int x){
f[x][0] = f[x][1] = 0;siz[x] = 1;
for(re int i = hd[x];i;i = e[i].nx){
int y = e[i].ver;
if(y == fa[x])continue;
fa[y] = x,dp(y),siz[x] += siz[y];
}
for(re int i = hd[x];i;i = e[i].nx){
int y = e[i].ver;
if(y == fa[x])continue;
for(re int j = min(K,siz[x]);j >= 0;j--)
for(re int k = 0;k <= min(j,siz[y]);k++)
if(f[x][j-k] >= 0)f[x][j] = max(f[x][j],f[x][j-k] + f[y][k] + (ll)(k * (K - k) + (siz[y] - k) * (n - K - siz[y] + k)) * e[i].ed);
}
}
int main(){
freopen("haoi2015_t1.in","r",stdin);
freopen("haoi2015_t1.out","w",stdout);
n = read(),K = read();
for(int i = 1;i < n;i++){
int x = read(),y = read();ll z = read();
link(x,y,z),link(y,x,z);
}
memset(f,-0x3f,sizeof f);
dp(1);
printf("%lld\n",f[1][K]);
return 0;
}