Submission #753281

#TimeUsernameProblemLanguageResultExecution timeMemory
753281LeonaRagingElection Campaign (JOI15_election_campaign)C++14
10 / 100
121 ms36276 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define db(val) "[" #val " = " << (val) << "] " const int maxn = 1e5 + 4; const int N = 16; struct Query { int u, v, w; Query(int u = 0, int v = 0, int w = 0): u(u), v(v), w(w) {} } q[maxn]; int n, m, h[maxn], tin[maxn], tout[maxn], up[maxn][20], dp[maxn], sum[maxn]; vector<int> adj[maxn], cross[maxn], down[maxn]; void dfs1(int u, int p) { tin[u] = ++tin[0]; for (int j = 1; j <= N; j++) up[u][j] = up[up[u][j - 1]][j - 1]; for (int v : adj[u]) if (v != p) { h[v] = h[u] + 1; up[v][0] = u; dfs1(v, u); } tout[u] = tin[0]; } bool isPar(int u, int p) { return tin[p] <= tin[u] && tout[p] >= tout[u]; } int jump(int u, int v) { for (int j = N; j >= 0; j--) if (up[u][j] && !isPar(v, up[u][j])) u = up[u][j]; return u; } void dfs2(int u, int p) { for (int v : adj[u]) if (v != p) { dfs2(v, u); sum[u] += dp[v]; } dp[u] = sum[u]; for (int i : cross[u]) { int a = q[i].u, b = q[i].v, w = q[i].w; dp[u] = max(dp[u], sum[u] - dp[jump(a, u)] - dp[jump(b, u)] + sum[a] + sum[b] + w); } for (int i : down[u]) { int v = q[i].v, w = q[i].w; dp[u] = max(dp[u], sum[u] - dp[jump(v, u)] + sum[v] + w); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1, u, v; i < n; i++) { cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } dfs1(1, 0); cin >> m; for (int i = 1, u, v, w; i <= m; i++) { cin >> u >> v >> w; if (h[u] > h[v]) swap(u, v); if (isPar(v, u)) down[u].pb(i); else cross[up[jump(u, v)][0]].pb(i); q[i] = Query(u, v, w); } dfs2(1, 0); cout << dp[1]; }
#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...