Submission #34838

#TimeUsernameProblemLanguageResultExecution timeMemory
34838sinhrivElection Campaign (JOI15_election_campaign)C++14
100 / 100
663 ms54716 KiB
#include <bits/stdc++.h> using namespace std; const int N = 100010; struct Query{ int u; int v; int w; }; /// tree int n, m, cnt; int L[N]; int R[N]; int depth[N]; int jump[N][22]; vector < int > adj[N]; void preDfs(int u, int p = 0){ L[u] = ++cnt; jump[u][0] = p; depth[u] = depth[p] + 1; for(int i = 1; i < 20; ++i){ jump[u][i] = jump[jump[u][i - 1]][i - 1]; } for(int v : adj[u]){ if(v == p) continue; preDfs(v, u); } R[u] = cnt; } int lca(int u, int v){ if(depth[u] < depth[v]) swap(u, v); for(int i = 19; i >= 0; --i){ if(depth[jump[u][i]] >= depth[v]) u = jump[u][i]; } if(u == v) return u; for(int i = 19; i >= 0; --i){ if(jump[u][i] != jump[v][i]){ u = jump[u][i]; v = jump[v][i]; } } return jump[u][0]; } int subTree(int u, int x){ for(int i = 19; i >= 0; --i){ if(depth[jump[x][i]] > depth[u]){ x = jump[x][i]; } } return x; } /// Dp int f[N]; vector < Query > solve[N]; struct SegmentTree{ int lazy[N << 3]; int value[N << 3]; void push(int x){ value[x] += lazy[x]; lazy[x + x] += lazy[x]; lazy[x + x + 1] += lazy[x]; lazy[x] = 0; } #define mid ((l + r) >> 1) void update(int x, int l, int r, int u, int v, int val){ push(x); if(l > v || r < u) return; if(l >= u && r <= v){ lazy[x] += val; push(x); return; } update(x + x, l, mid, u, v, val); update(x + x + 1, mid + 1, r, u, v, val); value[x] = value[x + x] + value[x + x + 1]; } int query(int x, int l, int r, int pos){ if(pos > r || pos < l) return 0; if(l == r) return value[x] + lazy[x]; return lazy[x] + query(x + x, l, mid, pos) + query(x + x + 1, mid + 1, r, pos); } }Smt; #define value(x) Smt.query(1, 1, n, L[x]) void dfs(int u, int p = 0){ int tot = 0; for(int v : adj[u]){ if(v == p) continue; dfs(v, u); tot += f[v]; } f[u] = max(f[u], tot); for(auto t : solve[u]){ int a = t.u, b = t.v; if(a == b && a == u){ f[u] = max(f[u], tot + t.w); continue; } if(b == u) swap(a, b); if(a == u){ int v = subTree(u, b); f[u] = max(f[u], tot - f[v] + value(b) + t.w); continue; } int i = subTree(u, a), j = subTree(u, b); f[u] = max(f[u], tot - f[i] - f[j] + value(a) + value(b) + t.w); } for(int v : adj[u]){ if(v == p) continue; Smt.update(1, 1, n, L[v], R[v], tot - f[v]); } Smt.update(1, 1, n, L[u], L[u], tot); } int main(){ if(fopen("1.inp", "r")){ freopen("1.inp", "r", stdin); } scanf("%d", &n); for(int i = 1; i < n; ++i){ int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } preDfs(1); scanf("%d", &m); for(int i = 1; i <= m; ++i){ int u, v, w; scanf("%d%d%d", &u, &v, &w); int p = lca(u, v); solve[p].push_back({u, v, w}); } dfs(1); cout << f[1]; return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:138:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen("1.inp", "r", stdin);
                               ^
election_campaign.cpp:142:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
election_campaign.cpp:145:24: 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:152:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
                 ^
election_campaign.cpp:155:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &u, &v, &w);
                              ^
#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...