제출 #1055347

#제출 시각아이디문제언어결과실행 시간메모리
1055347GrandTiger1729구슬과 끈 (APIO14_beads)C++17
0 / 100
1 ms348 KiB
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<vector<pair<int, int>>> g(n); for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; u--, v--; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } vector<bool> vis(n); vector<vector<int>> dp(n, vector<int>(2, -INF)); function<void(int, int)> dfs = [&](int u, int W) { vis[u] = true; int tot = 0; vector<int> res; for (auto &[v, w] : g[u]) { if (!vis[v]) { dfs(v, w); tot += max(dp[v][0], dp[v][1]); res.push_back(dp[v][0] + w - max(dp[v][0], dp[v][1])); } } sort(begin(res), end(res), greater<>()); dp[u][0] = tot; if (res.size() >= 2) { dp[u][0] = max(dp[u][0], tot + res[0] + res[1]); } if (res.size() >= 1) { dp[u][1] = tot + res[0] + W; } }; dfs(0, -INF); int ans = dp[0][0]; cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...