Submission #878991

#TimeUsernameProblemLanguageResultExecution timeMemory
87899112345678Election Campaign (JOI15_election_campaign)C++17
100 / 100
163 ms41832 KiB
#include <bits/stdc++.h> using namespace std; const int nx=1e5+5, kx=17; int n, m, a[nx], b[nx], c[nx], u, v, dp1[nx], dp2[nx], lvl[nx], pa[nx][kx], sm[nx]; vector<int> d[nx], l[nx]; void dfs(int u, int p) { lvl[u]=lvl[p]+1; pa[u][0]=p; for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1]; for (auto v:d[u]) if (v!=p) dfs(v, u); } int lca(int u, int v) { if (lvl[u]>lvl[v]) swap(u, v); for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i]; if (u==v) return v; for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i]; return pa[u][0]; } void dfs2(int u, int p) { for (auto v:d[u]) if (v!=p) dfs2(v, u), dp2[u]+=dp1[v]; dp1[u]=dp2[u]; for (auto i:l[u]) { int tmp=dp2[u], x, tmp2=0; stack<int> s; while (a[i]!=u) s.push(a[i]), a[i]=pa[a[i]][0]; while (!s.empty()) x=s.top(), s.pop(), tmp2+=sm[x], sm[x]=tmp2, pa[x][0]=u; tmp+=tmp2; tmp2=0; while (b[i]!=u) s.push(b[i]), b[i]=pa[b[i]][0]; while (!s.empty()) x=s.top(), s.pop(), tmp2+=sm[x], sm[x]=tmp2, pa[x][0]=u; dp1[u]=max(dp1[u], tmp+tmp2+c[i]); } sm[u]=dp2[u]-dp1[u]; //cout<<u<<' '<<dp1[u]<<' '<<dp2[u]<<' '<<sm[u]<<'\n'; } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n; for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u); dfs(1, 1); cin>>m; for (int i=1; i<=m; i++) cin>>a[i]>>b[i]>>c[i], l[lca(a[i], b[i])].push_back(i); dfs2(1, 1); cout<<dp1[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...