Submission #1120095

#TimeUsernameProblemLanguageResultExecution timeMemory
1120095luvnaElection Campaign (JOI15_election_campaign)C++14
100 / 100
258 ms62372 KiB
#include<bits/stdc++.h> #define MASK(i) (1 << (i)) #define pub push_back #define all(v) v.begin(), v.end() #define compact(v) v.erase(unique(all(v)), end(v)) #define pii pair<int,int> #define fi first #define se second #define endl "\n" #define dbg(x) "[" #x " = " << (x) << "]" using namespace std; template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;} template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;} typedef long long ll; typedef long double ld; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);} const int N = 1e5 + 15; const int lg = 17; struct node{ int u, v, w; }; struct FenwickTree{ int n; vector<ll> bit; FenwickTree(int n) : n(n), bit(n + 15, 0) {} void update(int idx, ll v){ for(;idx <= n; idx += (idx & (-idx))) bit[idx] += v; } ll get(int l, int r){ll res = 0; l--; for(;r;r -= (r & (-r))) res += bit[r]; for(;l;l -= (l & (-l))) res -= bit[l]; return res; } }; void solve(){ int n; cin >> n; vector<vector<int>> g(n+15); for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } vector<int> head(n+15, 0), hev(n + 15, 0); vector<vector<int>> par(n + 15, vector<int>(lg+2)); vector<int> tin(n + 15, 0), sz(n + 15, 0), h(n + 15, 0); int timerDFS = 0; auto dfs = [&](auto& dfs, int u) -> void{ sz[u] = 1; for(int v : g[u]) if(v != par[u][0]){ h[v] = h[u] + 1; par[v][0] = u; for(int j = 1; j <= lg; j++) par[v][j] = par[par[v][j-1]][j-1]; dfs(dfs, v); sz[u] += sz[v]; if(sz[v] > sz[hev[u]]) hev[u] = v; } }; auto hld = [&](auto& hld, int u, int cur_chain) -> void{ head[u] = cur_chain; tin[u] = ++timerDFS; if(hev[u]) hld(hld, hev[u], cur_chain); for(int v : g[u]) if(v != par[u][0] && v != hev[u]) hld(hld, v, v); }; dfs(dfs,1); hld(hld, 1, 1); FenwickTree bit(n); auto lca = [&](int u, int v) -> int{ for(;head[u] != head[v]; u = par[head[u]][0]){ if(h[head[u]] < h[head[v]]) swap(u,v); } return (h[u] < h[v]) ? u : v; }; //2 case: //lca(u,v) = u -> query on 1 subtree //lca(u,v) != u and v -> query on 2 subtree int m; cin >> m; vector<vector<pii>> evtOne(n + 15); vector<vector<node>> evtTwo(n + 15); while(m--){ int u, v, w; cin >> u >> v >> w; if(h[u] > h[v]) swap(u,v); int LCA = lca(u,v); //cout << u << " " << v << " " << lca(u,v) << endl; if(u == LCA) evtOne[LCA].push_back({v,w}); else evtTwo[LCA].push_back({u,v,w}); } auto get = [&](int u, int v) -> ll{ ll res = 0; for(;head[u] != head[v]; u = par[head[u]][0]){ if(h[head[u]] < h[head[v]]) swap(u,v); res += bit.get(tin[head[u]], tin[u]); } if(h[u] > h[v]) swap(u,v); res += bit.get(tin[u], tin[v]); return res; }; vector<ll> dp(n + 15, 0), sum(n + 15, 0); auto bin_lift = [&](int u, int v) -> int{ for(int j = lg; j >= 0; j--){ if(h[v] - (1 << j) > h[u]) v = par[v][j]; } return v; }; auto dfsDP = [&](auto& dfsDP, int u) -> void{ for(int v : g[u]) if(v != par[u][0]){ dfsDP(dfsDP, v); sum[u] += dp[v]; } //cout << "case: " << dbg(u) << endl << endl; dp[u] = sum[u]; //case 1: u = lca(u,v): sum[u] + sum[v] + w + cost(u -> v) for(auto &[v, w] : evtOne[u]){ ll cost = sum[u] + w; cost -= dp[bin_lift(u,v)]; //cout << v << " " << bin_lift(u,v) << endl; //cout << u << " " << v << " bel " << get(u,v) << endl; cost += get(u,v); cost += sum[v]; maximize(dp[u], cost); } //case 2: //add cost(u -> U) + cost(u -> V) //subtract dp[bin_lift(U and V)] // cost(u -> U) sum[u] - dp[v]: get down to v without adding dp[v] for(auto &[U, V, w] : evtTwo[u]){ ll cost = sum[u] + w; cost -= dp[bin_lift(u,U)]; cost -= dp[bin_lift(u,V)]; cost += sum[U]; cost += sum[V]; cost += get(u,U); cost += get(u,V); maximize(dp[u], cost); } //cout << dbg(u) << dbg(dp[u]) << dbg(sum[u]) << endl; for(int v : g[u]) if(v != par[u][0]){ bit.update(tin[v], sum[u] - dp[v]); } }; dfsDP(dfsDP, 1); cout << dp[1]; } signed main(){ ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); #define task "task" if(fopen(task".INP", "r")){ freopen(task".INP", "r", stdin); freopen(task".OUT", "w", stdout); } int t; t = 1; //cin >> t; while(t--) solve(); }

Compilation message (stderr)

election_campaign.cpp: In lambda function:
election_campaign.cpp:144:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  144 |         for(auto &[v, w] : evtOne[u]){
      |                   ^
election_campaign.cpp:158:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  158 |         for(auto &[U, V, w] : evtTwo[u]){
      |                   ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:188:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  188 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:189:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  189 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...