Submission #145290

#TimeUsernameProblemLanguageResultExecution timeMemory
145290dolphingarlicBeads and wires (APIO14_beads)C++14
100 / 100
467 ms32868 KiB
#include <bits/stdc++.h> #pragma GCC Optimize("O3") #define FOR(i, x, y) for (int i = x; i < y; i++) #define MOD 1000000007 typedef long long ll; using namespace std; #define N 234567 #define mp make_pair #define pb push_back #define INF 1000000000000LL int n, a, b; ll w, pw[N], dp[N][2][2]; vector<pair<int, ll> > al[N]; void dfs(int node, int par) { if (al[node].size() == 1 && par != -1) return; ll sc = 0, bst_mid = -INF, bst_unuse_mid = -INF, bst_unuse_mid2 = -INF, bst_unuse_nomid = -INF, bst_unuse_nomid2 = -INF; int bst_unuse_mid_child, bst_unuse_mid_child2, bst_unuse_nomid_child, bst_unuse_nomid_child2; for (int i = 0; i < al[node].size(); i++) if (al[node][i].first != par) { int child = al[node][i].first; ll edge = al[node][i].second; pw[child] = edge; dfs(child, node); ll normsc = max(dp[child][0][0], dp[child][1][0]); sc += normsc; ll midsc = max(dp[child][0][1], dp[child][1][1]) - normsc; if (midsc > bst_mid) bst_mid = midsc; ll unuse_midsc = dp[child][0][1] + edge - normsc; if (unuse_midsc > bst_unuse_mid) { bst_unuse_mid2 = bst_unuse_mid; bst_unuse_mid_child2 = bst_unuse_mid_child; bst_unuse_mid = unuse_midsc; bst_unuse_mid_child = child; } else if (unuse_midsc > bst_unuse_mid2) { bst_unuse_mid2 = unuse_midsc; bst_unuse_mid_child2 = child; } ll unuse_nomidsc = dp[child][0][0] + edge - normsc; if (unuse_nomidsc > bst_unuse_nomid) { bst_unuse_nomid2 = bst_unuse_nomid; bst_unuse_nomid_child2 = bst_unuse_nomid_child; bst_unuse_nomid = unuse_nomidsc; bst_unuse_nomid_child = child; } else if (unuse_nomidsc > bst_unuse_nomid2) { bst_unuse_nomid2 = unuse_nomidsc; bst_unuse_nomid_child2 = child; } } dp[node][0][0] = sc; dp[node][0][1] = sc + max(0LL, bst_mid); dp[node][0][1] = max(dp[node][0][1], sc + bst_unuse_nomid + bst_unuse_nomid2); if (bst_unuse_mid_child != bst_unuse_nomid_child) dp[node][0][1] = max(dp[node][0][1], sc + bst_unuse_nomid + bst_unuse_mid); else { dp[node][0][1] = max(dp[node][0][1], sc + bst_unuse_nomid2 + bst_unuse_mid); dp[node][0][1] = max(dp[node][0][1], sc + bst_unuse_nomid + bst_unuse_mid2); } dp[node][1][0] = sc + pw[node] + bst_unuse_nomid; dp[node][1][1] = sc + pw[node] + bst_unuse_mid; } int main() { cin >> n; for (int i = 0; i < n - 1; i++) { cin >> a >> b >> w; al[a].pb(mp(b, w)); al[b].pb(mp(a, w)); } dfs(1, -1); cout << max(dp[1][0][1], dp[1][0][0]) << '\n'; return 0; }

Compilation message (stderr)

beads.cpp:2:0: warning: ignoring #pragma GCC Optimize [-Wunknown-pragmas]
 #pragma GCC Optimize("O3")
 
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:22:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < al[node].size(); i++)
                     ~~^~~~~~~~~~~~~~~~~
beads.cpp:20:30: warning: variable 'bst_unuse_mid_child2' set but not used [-Wunused-but-set-variable]
     int bst_unuse_mid_child, bst_unuse_mid_child2, bst_unuse_nomid_child,
                              ^~~~~~~~~~~~~~~~~~~~
beads.cpp:21:9: warning: variable 'bst_unuse_nomid_child2' set but not used [-Wunused-but-set-variable]
         bst_unuse_nomid_child2;
         ^~~~~~~~~~~~~~~~~~~~~~
beads.cpp:57:5: warning: 'bst_unuse_nomid_child' may be used uninitialized in this function [-Wmaybe-uninitialized]
     if (bst_unuse_mid_child != bst_unuse_nomid_child)
     ^~
beads.cpp:57:5: warning: 'bst_unuse_mid_child' may be used uninitialized in this function [-Wmaybe-uninitialized]
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...