Submission #31739

#TimeUsernameProblemLanguageResultExecution timeMemory
31739top34051Beads and wires (APIO14_beads)C++14
0 / 100
6 ms4992 KiB
#include<bits/stdc++.h> using namespace std; #define maxn 200005 #define inf (long long)2e10 int n; int par[maxn]; long long mx[2], mem[maxn][2]; vector<int> p; vector<pair<int,long long> > from[maxn]; void dfs(int x) { int i,y; for(i=0;i<from[x].size();i++) { y = from[x][i].first; if(y==par[x]) continue; par[y] = x; dfs(y); } p.push_back(x); } void upd(long long val) { for(int i=0;i<2;i++) if(mx[i]<val) swap(mx[i],val); } main() { int i,x,y,now; long long val, sum, temp; scanf("%d",&n); for(i=0;i<n-1;i++) { scanf("%d%d%lld",&x,&y,&val); from[x].push_back({y,val}); from[y].push_back({x,val}); } par[1] = 0; dfs(1); for(now=0;now<p.size();now++) { x = p[now]; //Prep sum = 0; mx[0] = mx[1] = -inf; for(i=0;i<from[x].size();i++) { y = from[x][i].first; val = from[x][i].second; temp = max(mem[y][0], mem[y][1] + val); if(y!=par[x]) { sum += temp; upd(mem[y][0] + val - temp); } } //Don't Fix mem[x][0] = max(sum, sum + mx[0] + mx[1]); //Fix mem[x][1] = max(-inf, sum + mx[0]); // printf("mem %d : %lld and %lld\n",x,mem[x][0],mem[x][1]); } printf("%lld",mem[x][0]); }

Compilation message (stderr)

beads.cpp: In function 'void dfs(int)':
beads.cpp:14:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<from[x].size();i++) {
             ~^~~~~~~~~~~~~~~
beads.cpp: At global scope:
beads.cpp:27:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
beads.cpp: In function 'int main()':
beads.cpp:38:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(now=0;now<p.size();now++) {
               ~~~^~~~~~~~~
beads.cpp:43:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(i=0;i<from[x].size();i++) {
                 ~^~~~~~~~~~~~~~~
beads.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
     ~~~~~^~~~~~~~~
beads.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%lld",&x,&y,&val);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...