From a4f227393849291ee3ba536eff15020f27600ae3 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 7 Apr 2022 21:43:49 +0800 Subject: [PATCH] =?UTF-8?q?#10153.=20=E3=80=8C=E4=B8=80=E6=9C=AC=E9=80=9A?= =?UTF-8?q?=205.2=20=E4=BE=8B=201=E3=80=8D=E4=BA=8C=E5=8F=89=E8=8B=B9?= =?UTF-8?q?=E6=9E=9C=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://loj.ac/s/1435363 --- LibreOJ/10153/10153.cpp | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 LibreOJ/10153/10153.cpp diff --git a/LibreOJ/10153/10153.cpp b/LibreOJ/10153/10153.cpp new file mode 100644 index 00000000..ff22681f --- /dev/null +++ b/LibreOJ/10153/10153.cpp @@ -0,0 +1,45 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 105; + +int n, q, l[N], r[N], a[N], f[N][N]; +std::vector> g[N]; + +void dfs(int u, int fa) { + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (v == fa) continue; + dfs(v, u); + + for (int k = q; k; k--) { + for (int j = k - 1; j >= 0; j--) { + f[u][k] = std::max(f[u][k], w + f[u][k - j - 1] + f[v][j]); + } + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n >> q; + 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)); + } + + dfs(1, -1); + + cout << f[1][q] << endl; + + return 0; +}