Submission #128506

#TimeUsernameProblemLanguageResultExecution timeMemory
128506mirbek01Election Campaign (JOI15_election_campaign)C++11
100 / 100
367 ms31796 KiB
# include <bits/stdc++.h> using namespace std; const int N = 1e5 + 2; int n, m, a[N], b[N], c[N], dp[N], sv[N]; int up[17][N], tin[N], tout[N], timer, ver[N]; int t[N * 4], add[N * 4]; vector <int> g[N], qe[N]; void dfs(int v, int pr){ tin[v] = ++ timer; ver[timer] = v; up[0][v] = pr; for(int i = 1; i <= 16; i ++) up[i][v] = up[i - 1][up[i - 1][v]]; for(int to : g[v]){ if(to == pr) continue; dfs(to, v); } tout[v] = timer; } bool upper(int a, int b){ return tin[a] <= tin[b] && tout[a] >= tout[b]; } int lca(int a, int b){ if(upper(a, b)) return a; if(upper(b, a)) return b; for(int i = 16; i >= 0; i --){ if(!upper(up[i][a], b)) a = up[i][a]; } return up[0][a]; } void push(int v, int tl, int tr){ t[v] += (tr - tl + 1) * add[v]; if(tl != tr){ add[v << 1] += add[v]; add[v << 1 | 1] += add[v]; } add[v] = 0; } void upd(int l, int r, int val, int v = 1, int tl = 1, int tr = n){ push(v, tl, tr); if(l > tr || tl > r) return ; if(l <= tl && tr <= r){ add[v] += val; push(v, tl, tr); return ; } int tm = (tl + tr) >> 1; upd(l, r, val, v << 1, tl, tm); upd(l, r, val, v << 1 | 1, tm + 1, tr); t[v] = t[v << 1] + t[v << 1 | 1]; } int get(int pos, int v = 1, int tl = 1, int tr = n){ push(v, tl, tr); if(tl == tr) return t[v]; int tm = (tl + tr) >> 1; if(pos <= tm) return get(pos, v << 1, tl, tm); return get(pos, v << 1 | 1, tm + 1, tr); } void calc(int v, int pr){ for(int to : g[v]){ if(to == pr) continue; calc(to, v); sv[v] += dp[to]; } for(int to : g[v]){ if(to == pr) continue; upd(tin[to], tout[to], sv[v] - dp[to]); } dp[v] = sv[v]; for(int i : qe[v]){ int ret = sv[ a[i] ] + sv[ b[i] ] - sv[v] + c[i]; ret += get(tin[ a[i] ]); ret += get(tin[ b[i] ]); dp[v] = max(dp[v], ret); } } int main(){ scanf("%d", &n); for(int i = 1; i < n; i ++){ int u, v; scanf("%d %d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs(1, 1); scanf("%d", &m); for(int i = 1; i <= m; i ++){ scanf("%d %d %d", a+i, b+i, c+i); qe[ lca(a[i], b[i]) ].push_back(i); } calc(1, 1); cout << dp[1] << endl; }

Compilation message (stderr)

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:101:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", &n);
       ~~~~~^~~~~~~~~~
election_campaign.cpp:105:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &u, &v);
             ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:112:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", &m);
       ~~~~~^~~~~~~~~~
election_campaign.cpp:115:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d %d", a+i, b+i, c+i);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...