Submission #99791

#TimeUsernameProblemLanguageResultExecution timeMemory
99791Retro3014Beads and wires (APIO14_beads)C++17
100 / 100
257 ms25740 KiB
#include <bits/stdc++.h> #define pb push_back #define all(v) ((v).begin(), (v).end()) #define sortv(v) sort(all(v)) #define sz(v) ((int)(v).size()) #define uniqv(v) (v).erase(unique(all(v)), (v).end()) #define umax(a, b) (a)=max((a), (b)) #define umin(a, b) (a)=min((a), (b)) #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define rep(i,n) FOR(i,1,n) #define rep0(i,n) FOR(i,0,(int)(n)-1) #define FI first #define SE second #define INF 2000000000 #define INFLL 1000000000000000000LL using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, ll> pil; const int MAX_N = 200000; int N; vector<pil> gp[MAX_N+1]; int p[MAX_N+1]; int c[MAX_N+1]; ll dp[MAX_N+1][4]; void dfs(int x){ for(auto i : gp[x]){ if(i.first==p[x]) continue; p[i.first] = x; c[i.first] = i.second; dfs(i.first); } for(auto i : gp[x]) { if(i.first==p[x]) continue; dp[x][0] += max(dp[i.first][0], dp[i.first][2]); } ll mx = -INF; dp[x][1] = dp[x][2] = dp[x][3] = -INF; for(auto i : gp[x]){ if(i.first==p[x]) continue; mx = max(mx, max(dp[i.first][1], dp[i.first][3]) - max(dp[i.first][0], dp[i.first][2])); } dp[x][1] = dp[x][0] + mx; ll mx1 = -INF, mx2 = -INF; for(auto i : gp[x]){ if(i.first==p[x]) continue; dp[x][1] = max(dp[x][1], dp[x][0] + mx1 + dp[i.first][1] + i.second - max(dp[i.first][0], dp[i.first][2])); dp[x][1] = max(dp[x][1], dp[x][0] + mx2 + dp[i.first][0] + i.second - max(dp[i.first][0], dp[i.first][2])); dp[x][1] = max(dp[x][1], dp[x][0] + mx1 + dp[i.first][0] + i.second - max(dp[i.first][0], dp[i.first][2])); mx1 = max(mx1, dp[i.first][0] + i.second - max(dp[i.first][0], dp[i.first][2])); mx2 = max(mx2, dp[i.first][1] + i.second - max(dp[i.first][0], dp[i.first][2])); } for(auto i : gp[x]){ if(i.first==p[x]) continue; dp[x][2] = max(dp[x][2], dp[x][0] + dp[i.first][0] + i.second - max(dp[i.first][0], dp[i.first][2]) + c[x]); } for(auto i : gp[x]){ if(i.first==p[x]) continue; dp[x][3] = max(dp[x][3], c[x] + dp[x][0] + dp[i.first][1] + i.second - max(dp[i.first][0], dp[i.first][2])); } /*cout<<x<<endl; for(int i=0; i<4; i++){ cout<<dp[x][i]<<' '; }cout<<endl;*/ } int main(){ scanf("%d", &N); for(int i=0; i<N-1; i++){ int x, y; ll z; scanf("%d%d%lld", &x, &y, &z); gp[x].push_back({y, z}); gp[y].push_back({x, z}); } dfs(1); cout<<max(dp[1][0], dp[1][1]); return 0; }

Compilation message (stderr)

beads.cpp: In function 'int main()':
beads.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
beads.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%lld", &x, &y, &z);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...