Submission #1119753

#TimeUsernameProblemLanguageResultExecution timeMemory
1119753luvnaElection Campaign (JOI15_election_campaign)C++14
10 / 100
170 ms41548 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 sz(v) (int)(v).size() #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; }; 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<vector<int>> par(n + 15, vector<int>(lg+2,0)); vector<int> h(n + 15); auto dfs = [&](auto& dfs, int u, int p) -> void{ for(int v : g[u]) if(v != p){ 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,u); } }; auto lca = [&](int u, int v) -> int{ if(h[v] > h[u]) swap(u,v); for(int j = lg; j >= 0; j--) if(h[u] - (1 << j) >= h[v]){ u = par[u][j]; } if(u == v) return u; for(int j = lg; j >= 0; j--){ if(par[u][j] != par[v][j]){ u = par[u][j]; v = par[v][j]; } } return par[u][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; }; dfs(dfs,1,-1); int m; cin >> m; vector<vector<node>> evt(n + 15); while(m--){ int u, v, w; cin >> u >> v >> w; //cout << u << " " << v << " " << lca(u,v) << endl; evt[lca(u,v)].push_back({u,v,w}); } vector<vector<int>> dp(n + 15, vector<int>(3)); //dp[u][0] -> not pick u //dp[u][1] -> pick u //dp[u][0] = sum all max(dp[v][0], dp[v][1]) auto dfsDP = [&](auto& dfsDP, int u, int p) -> void{ int sum = 0; for(int v : g[u]) if(v != p){ dfsDP(dfsDP,v,u); sum += max(dp[v][0], dp[v][1]); } dp[u][0] = dp[u][1] = sum; //if(!evt[u].empty()) cout << "case: " << dbg(u) << endl; for(auto &[U, V, w] : evt[u]){ int ncost = sum + w; int fakeU = bin_lift(u,U); int fakeV = bin_lift(u,V); //cout << "--------" << endl; //cout << dbg(U) << dbg(fakeU) << endl; //cout << dbg(V) << dbg(fakeV) << endl; if(u != fakeU){ ncost -= max(dp[fakeU][0], dp[fakeU][1]); ncost += dp[U][0]; } if(u != fakeV){ ncost -= max(dp[fakeV][0], dp[fakeV][1]); ncost += dp[V][0]; } maximize(dp[u][1], ncost); } }; dfsDP(dfsDP,1,-1); cout << max(dp[1][0], dp[1][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:108:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  108 |         for(auto &[U, V, w] : evt[u]){
      |                   ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         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...