Submission #312754

#TimeUsernameProblemLanguageResultExecution timeMemory
312754hohohahaBeads and wires (APIO14_beads)C++14
0 / 100
4 ms4992 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define ii pair<int, int> #define iii pair<ii, int> #define mp make_pair #define eb emplace_back #define vi vector<int> #define vii vector<ii> #define viii vector<iii> #define fi first #define se second #define xx fi.fi #define yy fi.se #define zz se int n, ans, mxw[200005][2], dp[200005][2], tu[2], tv[2], pw[200005], p[200005]; vii G[200005]; void dfs(int u) { int mxr = LLONG_MIN/100LL; for(ii e: G[u]) { int v = e.fi, w = e.se; if(v!=p[u]) { p[v] = u; pw[v] = w; dfs(v); dp[u][0]+=dp[v][1]; int replace = dp[v][0] - dp[v][1] + w; mxr = max(mxr, replace); int temp = mxw[u][0], mx0 = mxw[u][0], mx1 = mxw[u][1]; if(!mx0 or replace>dp[mx0][0] - dp[mx0][1] + pw[mx0]) { mxw[u][1] = mxw[u][0]; mxw[u][0] = v; } else if(!mx1 or replace> dp[mx1][0] - dp[mx1][1] + pw[mx1]) { mxw[u][1] = v; } } } dp[u][1] = max(dp[u][0], pw[u] + mxr + dp[u][0]); cout<<u<<" "<<dp[u][1]<<endl; } void euler(int u) { ans = max(ans, dp[u][0]); for(ii e: G[u]) { int v = e.fi, w = e.se; if(v!=p[u]) { // reroot tu[0] = dp[u][0], tu[1] = dp[u][1], tv[0] = dp[v][0], tv[1] = dp[v][1]; int mxid = mxw[u][0]==v? mxw[u][1]: mxw[u][0]; int mx = max(dp[mxid][0] - dp[mxid][1] + pw[mxid], (!p[u]? LLONG_MIN/100LL: dp[p[u]][0] - dp[p[u]][1] + pw[u])); dp[u][0] = dp[u][0] - dp[v][1]; dp[u][1] = max(dp[u][0], dp[u][0] + mx + w); dp[v][0] = dp[v][1] = dp[v][0]+dp[u][1]; euler(v); //rollback dp[u][0] = tu[0], dp[u][1] = tu[1], dp[v][0] = tv[0], dp[v][1] = tv[1]; } } } signed main() { // freopen(".inp", "r", stdin); // freopen(".out", "w", stdout); // ios_base::sync_with_stdio(false); // cin.tie(NULL); // cout.tie(NULL); cin>>n; for(int i=1; i<=n-1; i++) { int u, v, w; cin>>u>>v>>w; G[u].eb(v, w); G[v].eb(u, w); } dfs(1); euler(1); cout<<ans; }

Compilation message (stderr)

beads.cpp: In function 'void dfs(long long int)':
beads.cpp:38:8: warning: unused variable 'temp' [-Wunused-variable]
   38 |    int temp = mxw[u][0], mx0 = mxw[u][0], mx1 = mxw[u][1];
      |        ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...