Submission #33970

#TimeUsernameProblemLanguageResultExecution timeMemory
33970mohammad_kilaniBeads and wires (APIO14_beads)C++14
0 / 100
11 ms9728 KiB
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define oo 2000000000 const int N = 300010; int n , dp[N][2]; vector< pair<int,int> > g[N]; int solve(int node,int prnt,int len,bool b){ if(dp[node][b] != -1) return dp[node][b]; int sum = 0 ; dp[node][b] = 0 ; vector<int> v; for(int i=0;i<g[node].size();i++){ if(g[node][i].first != prnt){ int cur = solve(g[node][i].first,node,g[node][i].second,0); int cur2 = solve(g[node][i].first,node,g[node][i].second,1); sum += cur2; v.push_back(cur - cur2 + g[node][i].second); } } dp[node][b] = sum; sort(v.begin(),v.end()); if(v.size() > 1) dp[node][b] = max(dp[node][b],v[v.size()-1] + v[v.size()-2] + sum); if(b){ if(v.size() > 2) dp[node][b] = max(dp[node][b],sum + len + v[v.size()-1] + v[v.size()-2] + v[v.size()-3]); else if(v.size() > 0) dp[node][b] = max(dp[node][b],sum + len + v[v.size()-1]); } return dp[node][b]; } int main() { //freopen("in.txt","r",stdin); scanf("%d",&n); for(int i=0;i<n-1;i++){ int u , v, w; scanf("%d%d%d",&u,&v,&w); g[u].push_back(make_pair(v,w)); g[v].push_back(make_pair(u,w)); } memset(dp,-1,sizeof(dp)); cout<< solve(1,-1,0,0) << endl; return 0; }

Compilation message (stderr)

beads.cpp: In function 'int solve(int, int, int, bool)':
beads.cpp:14:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[node].size();i++){
              ~^~~~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
beads.cpp:39:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d",&u,&v,&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...