Submission #31610

#TimeUsernameProblemLanguageResultExecution timeMemory
31610ngkan146Beads and wires (APIO14_beads)C++98
0 / 100
6 ms4992 KiB
#include <bits/stdc++.h> using namespace std; struct edge{ int v,w; edge(int v=0,int w=0): v(v), w(w) {} }; int n; vector <edge> G[200005]; int dp[200005][5]; inline int val1(int pos,int w){ return dp[pos][0] + w; } inline int val02(int pos,int w){ return max(dp[pos][0], dp[pos][2] + w); } void dfs(int u,int p){ for(int i=0;i<G[u].size();i++){ int v = G[u][i].v; if (v == p) continue; dfs(v,u); } // dp[i][0] for(int i=0;i<G[u].size();i++){ int v = G[u][i].v; if (v == p) continue; dp[u][0] += val02(v,G[u][i].w); } // dp[i][1] int fi02=0,se02=0,fiva02=0, seva02=0; for(int i=0;i<G[u].size();i++){ int v = G[u][i].v; int w = G[u][i].w; if (v == p) continue; if (!fi02 || val1(v,w) - val02(v,w) >= val1(fi02,fiva02) - val02(fi02,fiva02)) se02 = fi02, seva02 = fiva02, fi02 = v, fiva02 = w; else if (!se02 || val1(v,w) - val02(v,w) >= val1(se02,seva02) - val02(se02,seva02)) se02 = v, seva02 = w; } dp[u][1] = - (int) 1e9; if ((int)G[u].size() - (u != 1) > 1){ //cout << "u " << u << ' ' << fi02 << ' ' << se02 << endl; dp[u][1] = dp[u][0] - val02(fi02,fiva02) - val02(se02,seva02) + val1(fi02,fiva02) + val1(se02,seva02); for(int i=0;i<G[u].size();i++){ int v = G[u][i].v; int w = G[u][i].w; if (v == p) continue; if (v != fi02) dp[u][1] = max(dp[u][1], dp[u][0] - val02(fi02,fiva02) + val1(fi02,fiva02) - val02(v,w) + w + dp[v][1]); else dp[u][1] = max(dp[u][1], dp[u][0] - val02(se02,seva02) + val1(se02,seva02) - val02(v,w) + w + dp[v][1]); } } // dp[i][2] dp[u][2] = -(int)1e9; for(int i=0;i<G[u].size();i++){ int v = G[u][i].v; int w = G[u][i].w; if (v == p) continue; dp[u][2] = max(dp[u][2], dp[u][0] - max(dp[v][0], dp[v][2] + G[u][i].w) + dp[v][0] + G[u][i].w); } } int main(){ scanf("%d",&n); for(int i=1;i<n;i++){ int x,y,w; scanf("%d %d %d",&x,&y,&w); G[x].push_back(edge(y,w)); G[y].push_back(edge(x,w)); } dfs(1,1); cout << max(dp[1][0], dp[1][1]) << endl; //for(int i=1;i<=n;i++) // cout << "I " << i << ' ' << dp[i][0] <<' ' << dp[i][1] << ' ' << dp[i][2] << endl; } /* 10 4 10 2 1 2 21 1 3 13 6 7 1 7 9 5 2 4 3 2 5 8 1 6 55 6 8 34 */

Compilation message (stderr)

beads.cpp: In function 'void dfs(int, int)':
beads.cpp:17:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<G[u].size();i++){
                 ~^~~~~~~~~~~~
beads.cpp:23:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<G[u].size();i++){
                 ~^~~~~~~~~~~~
beads.cpp:30:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<G[u].size();i++){
                 ~^~~~~~~~~~~~
beads.cpp:48:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<G[u].size();i++){
                     ~^~~~~~~~~~~~
beads.cpp:60:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<G[u].size();i++){
                 ~^~~~~~~~~~~~
beads.cpp:62:13: warning: unused variable 'w' [-Wunused-variable]
         int w = G[u][i].w;
             ^
beads.cpp: In function 'int main()':
beads.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
     ~~~~~^~~~~~~~~
beads.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d",&x,&y,&w);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...