0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 18:38:47 +00:00

#2124. 「HAOI2015」树上染色

This commit is contained in:
Baoshuo Ren 2022-06-11 18:34:23 +08:00
parent 2971a83e2f
commit f029b8860e
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68
21 changed files with 125 additions and 0 deletions

65
LibreOJ/2124/2124.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <iostream>
#include <cstring>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2005;
int n, k, fa[N], siz[N];
long long f[N][N];
std::vector<std::pair<int, int>> g[N];
void dfs(int u, int fa) {
siz[u] = 1;
f[u][0] = f[u][1] = 0;
for (auto e : g[u]) {
int v = e.first,
w = e.second;
if (v == fa) continue;
dfs(v, u);
siz[u] += siz[v];
for (int i = std::min(k, siz[u]); ~i; i--) {
if (~f[u][i]) {
f[u][i] += f[v][0] + 1ll * siz[v] * (n - k - siz[v]) * w;
}
for (int j = std::min(i, siz[v]); j; j--) {
if (~f[u][i - j]) {
f[u][i] = std::max(f[u][i], f[u][i - j] + f[v][j] + 1ll * (j * (k - j) + (siz[v] - j) * (n - k - siz[v] + j)) * w);
}
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
k = std::max(k, n - k);
for (int i = 1, u, v, w; i < n; i++) {
cin >> u >> v >> w;
g[u].push_back(std::make_pair(v, w));
g[v].push_back(std::make_pair(u, w));
}
memset(f, 0xff, sizeof(f));
dfs(1, 0);
cout << f[1][k] << endl;
return 0;
}

BIN
LibreOJ/2124/data/data1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2124/data/data9.out (Stored with Git LFS) Normal file

Binary file not shown.