Submission #187683

#TimeUsernameProblemLanguageResultExecution timeMemory
187683dndhkElection Campaign (JOI15_election_campaign)C++14
100 / 100
676 ms61896 KiB
#include <bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAX_N = 100000; const ll INFLL = 10000; const int MAX_K = 20; int N, M; vector<int> gp[MAX_N+1]; int lv[MAX_N+1], p[MAX_N+1], up[MAX_N+1][MAX_K+1]; vector<pii> vt[MAX_N+1]; void dfs(int x){ up[x][0] = p[x]; for(int i=1; i<MAX_K; i++) up[x][i] = up[up[x][i-1]][i-1]; for(int i=0; i<MAX_K; i++) vt[up[x][i]].pb({x, i}); for(int i : gp[x]){ if(i==p[x]) continue; lv[i] = lv[x]+1; p[i] = x; dfs(i); } } int lca(int x, int y){ for(int i=MAX_K-1; i>=0; i--){ if(lv[up[x][i]]>=lv[y]) x = up[x][i]; if(lv[up[y][i]]>=lv[x]) y = up[y][i]; } if(x==y) return x; for(int i=MAX_K-1; i>=0; i--){ if(up[x][i]!=up[y][i]) { x = up[x][i]; y = up[y][i]; } } return up[x][0]; } int dist(int x, int y){ int ret = 0; for(int i=MAX_K-1; i>=0; i--){ if(lv[up[x][i]]>=lv[y]) { x = up[x][i]; ret+=(1<<i); } if(lv[up[y][i]]>=lv[x]) { y = up[y][i]; ret+=(1<<i); } } if(x==y) return ret; for(int i=MAX_K-1; i>=0; i--){ if(up[x][i]!=up[y][i]) { x = up[x][i]; y = up[y][i]; ret+=(1<<(i+1)); } } return ret+2; } int upk(int x, int y){ for(int i=MAX_K-1; i>=0; i--){ if(y>=(1<<i)){ x = up[x][i]; y-=(1<<i); } } return x; } struct Q{ int a, b, c; }; vector<Q> query[MAX_N+1]; int dp1[MAX_N+1], dp2[MAX_N+1], dp3[MAX_N+1]; int cost[MAX_N+1][MAX_K+1]; void calc(int x, int y){ if(y==0){ cost[x][y] = dp2[x]-dp1[x]; }else{ cost[x][y] = cost[x][y-1] + cost[up[x][y-1]][y-1]; } } int calc2(int x, int y){ int ret = 0; for(int i=MAX_K-1; i>=0; i--){ if(lv[up[x][i]]>=lv[y]){ ret += cost[x][i]; x = up[x][i]; } } return ret; } void dfs2(int x){ for(int i : gp[x]){ if(i==p[x]) continue; dfs2(i); dp2[x]+=dp1[i]; } for(pii p : vt[x]){ calc(p.first, p.second); } dp3[x] = 0; for(Q q : query[x]){ int sum = dp2[x]; if(q.a!=x) sum+=calc2(q.a, x); if(q.b!=x) sum+=calc2(q.b, x); dp3[x] = max(dp3[x], sum+q.c); } dp1[x] = max(dp2[x], dp3[x]); } int main(){ scanf("%d", &N); for(int i=1; i<N; i++){ int a, b; scanf("%d%d", &a, &b); gp[a].pb(b); gp[b].pb(a); } lv[1] = 1;dfs(1); scanf("%d", &M); for(int i=1; i<=M; i++){ int a, b, c; scanf("%d%d%d", &a, &b, &c); query[lca(a, b)].pb({a, b, c}); } dfs2(1); cout<<dp1[1]; return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:126:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:128:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b; scanf("%d%d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:132:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &M);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:134:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b, c; scanf("%d%d%d", &a, &b, &c);
                ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...