Submission #412707

#TimeUsernameProblemLanguageResultExecution timeMemory
412707LastRoninBeads and wires (APIO14_beads)C++14
0 / 100
1 ms460 KiB
#include <bits/stdc++.h> #define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define ll long long #define pb push_back #define mp make_pair #define f first #define s second #define pill pair<ll, ll> using namespace std; const ll N = 1e4 + 10; const ll big = 1e9; ll n; vector<pair<int,int>> g[N]; int dp[N][2]; int dfs(int v, int p, int z) { dp[v][0] = dp[v][1] = 0; int dp2[2][3]; dp2[0][0] = 0, dp2[0][1] = -big, dp2[0][2] = -big; for(auto u : g[v]) { if(u.f != p) { dfs(u.f, v, u.s); dp2[1][0] = dp2[0][0] + max(dp[u.f][0], dp[u.f][1]); dp2[1][1] = max(dp2[0][1] + max(dp[u.f][0], dp[u.f][1]), dp2[0][0] + dp[u.f][0] + u.s); dp2[1][2] = max(dp2[0][2] + max(dp[u.f][0], dp[u.f][1]), dp2[0][1] + dp[u.f][0] + u.s); dp2[0][0] = dp2[1][0]; dp2[0][1] = dp2[1][1]; dp2[0][2] = dp2[1][2]; } } dp[v][0] = dp2[0][0]; dp[v][1] = dp2[0][1] + z; return dp2[0][2]; } int main() { speed; cin >> n; for(int i = 1, a, b, c; i < n; i++) cin >> a >> b >> c, g[a].pb(mp(b, c)), g[b].pb(mp(a, c)); int ans = 0; for(int i = 1; i <= n/2; i++) ans = max(ans, dfs(i, 0, 0)); cout << ans; } /* */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...