Submission #670832

#TimeUsernameProblemLanguageResultExecution timeMemory
670832ArnchBeads and wires (APIO14_beads)C++17
57 / 100
211 ms28568 KiB
#include<bits/stdc++.h> using namespace std; #define Sz(x) (x).size() #define All(x) (x).begin(), (x).end() const int N = 1e5 + 10; const long long INF = 1e18; long long dp[N][4]; vector<pair<int, int> > adj[N]; void DFS(int x, int p = 0, int cnt = 0) { for(auto [j, t] : adj[x]) { if(j == p) continue; DFS(j, x, t); } if(Sz(adj[x]) == 1 && x != 1) { return; } vector<long long> vc; long long sum = 0; for(auto [j, t] : adj[x]) { if(j == p) continue; sum += dp[j][1]; } // ba babash long long mx = -INF, mx2 = -INF; for(auto [j, t] : adj[x]) { if(j == p) continue; mx = max(mx, max(dp[j][2], dp[j][3]) + t - dp[j][1]); mx2 = max(mx2, dp[j][3] + t - dp[j][1]); } dp[x][0] = max(dp[x][0], sum + mx + cnt); dp[x][1] = max(dp[x][1], sum + mx2 + cnt); // ba bachash long long ind = 0; mx = -INF; for(auto [j, t] : adj[x]) { if(j == p) continue; if((dp[j][2] - dp[j][1] + t >= mx) || ind == 0) { mx = dp[j][2] - dp[j][1] + t; ind = j; } } vc.clear(); for(auto [j, t] : adj[x]) { if(j == ind || j == p) continue; vc.push_back(dp[j][3] + t - dp[j][1]); } sort(All(vc), greater<long long>()); if(Sz(vc) > 0) { dp[x][0] = max(dp[x][0], sum + mx + vc[0]); dp[x][2] = max(dp[x][2], sum + mx + vc[0]); } if(Sz(vc) > 1) { dp[x][0] = max(dp[x][0], sum + vc[1] + vc[0]); dp[x][2] = max(dp[x][2], sum + vc[1] + vc[0]); } ind = 0; mx = -INF; for(auto [j, t] : adj[x]) { if(j == p) continue; if((dp[j][3] - dp[j][1] + t >= mx) || ind == 0) { mx = dp[j][3] - dp[j][1] + t; ind = j; } } vc.clear(); for(auto [j, t] : adj[x]) { if(j == ind || j == p) continue; vc.push_back(dp[j][2] + t - dp[j][1]); } sort(All(vc), greater<long long>()); if(Sz(vc) > 0) { dp[x][0] = max(dp[x][0], sum + mx + vc[0]); dp[x][2] = max(dp[x][2], sum + mx + vc[0]); } // hich kodom mx = -INF; for(auto [j, t] : adj[x]) { if(j == p) continue; mx = max(mx, dp[j][0] - dp[j][1]); } for(int i = 0; i < 4; i++) dp[x][i] = max(dp[x][i], sum); dp[x][0] = max(dp[x][0], sum + mx); dp[x][2] = max(dp[x][2], sum + mx); } int main() { int n; cin >>n; for(int i = 0; i < n - 1; i++) { int u, v, c; cin >>u >>v >>c; adj[u].push_back({v, c}), adj[v].push_back({u, c}); } DFS(1); /*for(int i = 1; i <= n; i++) { cout<<"^^" <<i <<' ' <<dp[i][0] <<' ' <<dp[i][1] <<' ' <<dp[i][2] <<' ' <<dp[i][3] <<endl; }*/ cout<<dp[1][2] <<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...