Submission #968549

#TimeUsernameProblemLanguageResultExecution timeMemory
968549Neco_arcElection Campaign (JOI15_election_campaign)C++17
100 / 100
117 ms39876 KiB
#include <bits/stdc++.h> #define ll long long #define fi(i, a, b) for(int i = a; i <= b; i++) #define fid(i, a, b) for(int i = a; i >= b; i--) #define maxn (int) 2e5 + 7 using namespace std; int n, m; int cha[maxn][20]; ll dp[maxn], Sum[maxn]; struct dl { int x, y, w; } a[maxn]; vector<int> edges[maxn], store[maxn]; int In[maxn], Out[maxn]; void pre_dfs(int u, int par) { static int Time = 0; In[u] = ++Time; for(int v : edges[u]) if(v != par) { cha[v][0] = u; fi(i, 1, 17) cha[v][i] = cha[cha[v][i - 1]][i - 1]; pre_dfs(v, u); } Out[u] = Time; } bool insubtree(int u, int p) { return p == 0 || In[p] <= In[u] && In[u] <= Out[p]; } int lca(int u, int v) { if(insubtree(u, v)) return v; if(insubtree(v, u)) return u; fid(i, 17, 0) if(!insubtree(v, cha[u][i])) u = cha[u][i]; return cha[u][0]; } struct BIT { ll bit[maxn]; void update(int l, int r, ll val) { for(; l < maxn; l += (l & -l)) bit[l] += val; for(++r; r < maxn; r += (r & -r)) bit[r] -= val; } ll get(int x, ll ans = 0) { for(; x > 0; x -= (x & -x)) ans += bit[x]; return ans; } } Bit; void dfs(int u, int par) { for(int v : edges[u]) if(v != par) dfs(v, u), Sum[u] += dp[v]; dp[u] = Sum[u]; for(int id : store[u]) { ll val = a[id].w; if(a[id].x != u) val += Bit.get(In[a[id].x]); if(a[id].y != u) val += Bit.get(In[a[id].y]); dp[u] = max(dp[u], Sum[u] + val); } Bit.update(In[u], Out[u], Sum[u] - dp[u]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; fi(i, 1, n - 1) { int x, y; cin >> x >> y; edges[x].push_back(y), edges[y].push_back(x); } pre_dfs(1, 0); cin >> m; fi(i, 1, m) { int x, y, w; cin >> x >> y >> w; store[lca(x, y)].push_back(i); a[i] = {x, y, w}; } dfs(1, 0); cout << dp[1]; return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'bool insubtree(int, int)':
election_campaign.cpp:28:64: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   28 | bool insubtree(int u, int p) { return p == 0 || In[p] <= In[u] && In[u] <= Out[p]; }
      |                                                 ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
#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...