Submission #187634

#TimeUsernameProblemLanguageResultExecution timeMemory
187634dndhkElection Campaign (JOI15_election_campaign)C++14
20 / 100
1067 ms29364 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]; 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 : 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]; void dfs2(int x){ for(int i : gp[x]){ if(i==p[x]) continue; dfs2(i); dp2[x]+=dp1[i]; } dp3[x] = 0; for(Q q : query[x]){ int t = q.a; int sum = 0; while(t!=x){ sum+=dp2[t]-dp1[t]; t = p[t]; } t = q.b; while(t!=x){ sum+=dp2[t]-dp1[t]; t=p[t]; } dp3[x] = max(dp3[x], dp2[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:110:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:112: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:116:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &M);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:118: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...