Submission #56841

#TimeUsernameProblemLanguageResultExecution timeMemory
56841BruteforcemanElection Campaign (JOI15_election_campaign)C++11
100 / 100
622 ms143952 KiB
#include <bits/stdc++.h> using namespace std; vector <int> g[100010]; int anc[20][100010]; const int logn = 19; int dep[100010]; vector <int> path[100010]; int a[100010], b[100010], c[100010]; void dfs(int x, int par) { anc[0][x] = par; for(int i = 1; i <= logn; i++) { anc[i][x] = anc[i - 1][anc[i - 1][x]]; } for(auto i : g[x]) { if(i - par) { dep[i] = 1 + dep[x]; dfs(i, x); } } } int lca(int p, int q) { if(dep[p] > dep[q]) swap(p, q); for(int i = logn; i >= 0; i--) { if(dep[q] - (1 << i) >= dep[p]) { q = anc[i][q]; } } if(p == q) return p; for(int i = logn; i >= 0; i--) { if(anc[i][p] != anc[i][q]) { p = anc[i][p]; q = anc[i][q]; } } return anc[0][p]; } long long dp[100010]; long long sum[100010]; long long sib[100010]; long long l[100010], r[100010]; int tym; int n; void order(int x, int par) { l[x] = ++tym; for(int i : g[x]) { if(i - par) { order(i, x); } } r[x] = tym; } long long t[100010]; void update(int x, long long val) { for(int i = x; i <= n; i += i & (-i)) { t[i] += val; } } long long query(int x) { long long ans = 0; for(int i = x; i > 0; i -= i & (-i)) { ans += t[i]; } return ans; } long long query(int l, int r) { return query(r) - query(l - 1); } int get(int x, int p) { for(int i = logn; i >= 0; i--) { if(dep[x] - (1 << i) >= p) { x = anc[i][x]; } } return x; } void calc(int x, int par) { dp[x] = 0; for(auto i : g[x]) { if (i - par) { calc(i, x); dp[x] += dp[i]; } } sum[x] = dp[x]; for(auto i : g[x]) { if(i - par) { sib[i] = sum[x] - dp[i]; } } for(auto i : path[x]) { int node = get(a[i], dep[x] + 1); long long res = c[i]; res += query(l[a[i]]); if(node != x) res -= dp[node]; node = get(b[i], dep[x] + 1); res += query(l[b[i]]); if(node != x) res -= dp[node]; res += sum[x]; if(a[i] != x) res += sum[a[i]]; if(b[i] != x) res += sum[b[i]]; dp[x] = max(dp[x], res); } for(auto i : g[x]) { if(i - par) { update(l[i], sib[i]); update(r[i] + 1, -sib[i]); } } } int main(int argc, char const *argv[]) { int m; scanf("%d", &n); for(int i = 1; i < n; i++) { int p, q; scanf("%d %d", &p, &q); g[p].emplace_back(q); g[q].emplace_back(p); } dfs(1, 0); order(1, 0); scanf("%d", &m); for(int i = 1; i <= m; i++) { scanf("%d %d %d", &a[i], &b[i], &c[i]); path[lca(a[i], b[i])].emplace_back(i); } calc(1, 0); printf("%lld\n", dp[1]); return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'int main(int, const char**)':
election_campaign.cpp:118:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:121:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &p, &q);
   ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:127:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:129:8: 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...