Submission #32157

#TimeUsernameProblemLanguageResultExecution timeMemory
32157minchurlBeads and wires (APIO14_beads)C++11
0 / 100
6 ms4992 KiB
#include<stdio.h> #include<math.h> #include<vector> #define LL long long #define MAX_N 200005 #define MAX(x,y) ((x)>(y)?(x):(y)) using namespace std; struct emp{ LL y,cost; bool operator < (emp d) const{ return cost>d.cost; } }; LL n; LL master[MAX_N],branch[MAX_N]; vector<emp> net[MAX_N]; void make_branch(LL x,LL pa,LL cost){ LL i,sz; LL maxx=0,y; sz=net[x].size(); for(i=0;i<sz;i++){ if(net[x][i].y==pa) continue; maxx+=branch[net[x][i].y]; } maxx=MAX(maxx,branch[x]); for(i=0;i<sz;i++){ if(net[x][i].y==pa) continue; y=branch[net[x][i].y]+cost+net[x][i].cost+branch[x]; maxx=MAX(maxx,y); } branch[pa]+=maxx; } void merge(LL x,LL pa,LL cost){ LL i,sum,sz; emp max_branch[3],max_master[3]; max_branch[0].cost=max_branch[1].cost=-1; max_branch[0].y=max_branch[1].y=0; max_master[0].cost=max_master[1].cost=-1; max_master[0].y=max_master[1].y=0; sz=net[x].size(); if(pa && sz<3) return; if(!pa && sz<2) return; for(i=0;i<sz;i++){ if(net[x][i].y==pa) continue; if(branch[net[x][i].y]+net[x][i].cost>max_branch[0].cost){ max_branch[1]=max_branch[0]; max_branch[0].cost=branch[net[x][i].y]+net[x][i].cost; max_branch[0].y=net[x][i].y; }else if(branch[net[x][i].y]+net[x][i].cost>max_branch[1].cost){ max_branch[1].cost=branch[net[x][i].y]+net[x][i].cost; max_branch[1].y=net[x][i].y; } } for(i=0;i<sz;i++){ if(net[x][i].y==pa) continue; if(master[net[x][i].y]+net[x][i].cost>max_master[0].cost){ max_master[1]=max_master[0]; max_master[0].cost=master[net[x][i].y]+net[x][i].cost; max_master[0].y=net[x][i].y; }else if(master[net[x][i].y]+net[x][i].cost>max_master[1].cost){ max_master[1].cost=master[net[x][i].y]+net[x][i].cost; max_master[1].y=net[x][i].y; } } if(max_master[0].y-max_branch[0].y){ master[x]=MAX(master[x],max_master[0].cost+max_branch[0].cost); }else{ master[x]=MAX(master[x],MAX(max_master[0].cost+max_branch[1].cost,max_master[1].cost+max_branch[0].cost)); } } void make_master(LL x,LL pa,LL cost){ LL i,sz; sz=net[x].size(); for(i=0;i<sz;i++){ if(net[x][i].y-pa) make_master(net[x][i].y,x,net[x][i].cost); master[x]=MAX(master[x],master[net[x][i].y]); } make_branch(x,pa,cost); merge(x,pa,cost); //printf("%lld : %lld %lld\n",x,master[x],branch[x]); } int main(){ LL i,x,y,cost; emp node; scanf("%lld",&n); for(i=1;i<n;i++){ scanf("%lld %lld %lld",&x,&y,&cost); node.y=y; node.cost=cost; net[x].push_back(node); node.y=x; net[y].push_back(node); } make_master(1,0,0); printf("%lld\n",master[1]); return 0; }

Compilation message (stderr)

beads.cpp: In function 'void merge(long long int, long long int, long long int)':
beads.cpp:34:7: warning: unused variable 'sum' [-Wunused-variable]
  LL i,sum,sz;
       ^~~
beads.cpp: In function 'int main()':
beads.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld",&n);
  ~~~~~^~~~~~~~~~~
beads.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld %lld",&x,&y,&cost);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...