Submission #362958

#TimeUsernameProblemLanguageResultExecution timeMemory
362958ijxjdjdBeads and wires (APIO14_beads)C++14
0 / 100
4 ms5100 KiB
#include <bits/stdc++.h> #define FR(i, N) for (int i = 0; i < int(N); i++) #define all(x) begin(x), end(x) #define f first #define s second using namespace std; using ll = long long; const int MAXN = (int)(2e5) + 5; vector<pair<int, int>> adj[MAXN]; int dp[MAXN][2]; void dfs(int n, int p, int w) { vector<int> tot; vector<int> tot2; int allSum = 0; int allSum2 = 0; for (auto& e : adj[n]) { if (e.f != p) { dfs(e.f, n, e.s); tot2.push_back(e.s); tot.push_back(e.s + dp[e.f][0] - dp[e.f][1]); allSum += dp[e.f][1]; allSum2 += dp[e.f][0]; } } sort(all(tot)); reverse(all(tot)); sort(all(tot2)); reverse(all(tot2)); dp[n][0] = allSum; dp[n][1] = allSum; if (tot.size() > 0) { dp[n][1] = max(dp[n][1], allSum + tot[0]); } if (tot2.size() > 1) { dp[n][0] = max(dp[n][0], allSum2 + tot2[0] + tot2[1]); } dp[n][1] = max(dp[n][1], dp[n][0]); } int main() { cin.tie(0); cin.sync_with_stdio(0); int N; cin >> N; FR(i, N-1) { int u, v, w; cin >> u >> v >> w; u--, v--; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } dfs(0, 0, 0); cout << dp[0][0] << '\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...