Submission #96170

#TimeUsernameProblemLanguageResultExecution timeMemory
96170Retro3014Beads and wires (APIO14_beads)C++17
13 / 100
7 ms5120 KiB
#include <iostream> #include <vector> #include <algorithm> #include <stdio.h> #define MAX_N 200000 #define INF 2000000000000000LL using namespace std; typedef long long ll; int N; int ans; struct S{ S (int idx, ll data) : idx(idx), data(data) {} int idx;ll data; }; vector<S> gp[MAX_N+1]; vector<S> p; ll dp[MAX_N+1][4]; void dfs(int x){ for(int i=0; i<gp[x].size(); i++){ if(gp[x][i].idx!=p[x].idx){ p[gp[x][i].idx].idx = x; p[gp[x][i].idx].data = gp[x][i].data; dfs(gp[x][i].idx); } } ll sum = 0; for(int i=0; i<gp[x].size(); i++){ if(gp[x][i].idx==p[x].idx) continue; int now = gp[x][i].idx; dp[x][0] += max(dp[now][0], max(dp[now][1], dp[now][2])); dp[x][1] += max(dp[now][0], max(dp[now][1], dp[now][3])); sum += max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])); } ll mx1 = 0, mx2 = 0; dp[x][2] = dp[x][3] = -INF; bool tf = true; for(int i=0; i<gp[x].size(); i++){ if(gp[x][i].idx==p[x].idx) continue; if(tf){ tf = false; mx1 = dp[gp[x][i].idx][0] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])); mx2 = dp[gp[x][i].idx][1] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])); }else{ dp[x][1] = max(dp[x][1], sum + mx1 + gp[x][i].data + dp[gp[x][i].idx][0] - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]))); dp[x][1] = max(dp[x][1], sum + mx1 + dp[gp[x][i].idx][1] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]))); dp[x][1] = max(dp[x][1], sum + mx2 + gp[x][i].data + dp[gp[x][i].idx][0] - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]))); mx1 = max(mx1, dp[gp[x][i].idx][0] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]))); mx2 = max(mx2, dp[gp[x][i].idx][1] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]))); } dp[x][2] = max(dp[x][2], dp[gp[x][i].idx][1] + sum - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])) + gp[x][i].data + p[x].data); dp[x][3] = max(dp[x][3], dp[gp[x][i].idx][0] + sum - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])) + gp[x][i].data + p[x].data); dp[x][2] = max(dp[x][2], dp[x][3]); } return; } int main(){ scanf("%d", &N); for(int i=0; i<=N; i++) p.push_back({0, 0}); for(int i=0; i<N-1; i++){ int a, b;ll c; scanf("%d%d%lld", &a, &b, &c); gp[a].push_back({b, c}); gp[b].push_back({a, c}); } dfs(1); printf("%lld", max(dp[1][0], dp[1][1])); return 0; }

Compilation message (stderr)

beads.cpp: In function 'void dfs(int)':
beads.cpp:22:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
beads.cpp:30:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
beads.cpp:40:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
beads.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%lld", &a, &b, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...