Submission #362957

#TimeUsernameProblemLanguageResultExecution timeMemory
362957ijxjdjdBeads and wires (APIO14_beads)C++14
0 / 100
5 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; int allSum = 0; for (auto& e : adj[n]) { if (e.f != p) { dfs(e.f, n, e.s); tot.push_back(e.s + dp[e.f][0] - dp[e.f][1]); allSum += dp[e.f][1]; } } sort(all(tot)); reverse(all(tot)); dp[n][0] = allSum; dp[n][1] = allSum; for (int i = 0; i < min(2, int(tot.size())); i++) { allSum += tot[i]; if (i%2 == 0) { dp[n][1] = max(dp[n][1], allSum + w); } else { dp[n][0] = max(dp[n][0], allSum); } } 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...