Submission #512502

#TimeUsernameProblemLanguageResultExecution timeMemory
512502Killer2501Election Campaign (JOI15_election_campaign)C++14
100 / 100
253 ms37192 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define ull unsigned long long #define pb push_back #define pll pair<ll, ll> #define pii pair<int, int> #define fi first #define se second using namespace std; const int N = 2e5+5; const int M = (1<<17); const ll inf = 1e15; const int mod = 1e9+7; int n, t, k, a[N], c[N], d[N], m, P[N][19], h[N], in[N], out[N]; ll ans, tong, b[N], dp[N], fe[N]; string s; struct node { int val, x, y; node(){} node(int _val, int _x, int _y) { val = _val; x = _x; y = _y; } bool operator < (const node& u) const { return x > u.x; } }; vector<node> g[N]; vector<int> adj[N]; void add(int id, ll x) { for(; id <= n; id += id & -id)fe[id] += x; } void add(int l, int r, ll x) { add(l, x); add(r+1, -x); } ll get(int id) { ll total = 0; for(; id; id -= id & -id)total += fe[id]; return total; } void dfs(int u, int p = 0) { in[u] = ++k; for(int v: adj[u]) { if(v == p)continue; h[v] = h[u]+1; P[v][0] = u; for(int i = 1; i <= 18; i ++)P[v][i] = P[P[v][i-1]][i-1]; dfs(v, u); } out[u] = k; } int lca(int u, int v) { if(h[u] < h[v])swap(u, v); int log = log2(h[u]); for(int i = log; i >= 0; i --)if(h[u] >= h[v]+(1<<i))u = P[u][i]; if(u == v)return u; for(int i = log; i >= 0; i --) { if(P[u][i] && P[u][i] != P[v][i]) { u = P[u][i]; v = P[v][i]; } } return P[u][0]; } void cal(int u, int p = 0) { for(int v: adj[u]) { if(v == p)continue; cal(v, u); add(in[u], out[u], dp[v]); add(in[v], out[v], -dp[v]); } dp[u] = get(in[u]) - get(in[p]); for(node t: g[u]) { dp[u] = max(dp[u], get(in[t.x]) + get(in[t.y]) - get(in[u]) - get(in[p]) + t.val); } } void sol() { cin >> n; for(int i = 1; i < n; i ++) { int x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } dfs(1); cin >> m; while(m -- > 0) { int x, y, z; cin >> x >> y >> k; z = lca(x, y); g[z].pb(node(k, x, y)); } cal(1); cout << dp[1]; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); #define task "test" if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } int ntest = 1; //cin >> ntest; while(ntest -- > 0) sol(); }

Compilation message (stderr)

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:126:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:127:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |   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...