Submission #878989

#TimeUsernameProblemLanguageResultExecution timeMemory
87898912345678Election Campaign (JOI15_election_campaign)C++17
20 / 100
1040 ms30552 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]; 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]; while (a[i]!=u) tmp+=dp2[a[i]]-dp1[a[i]], a[i]=pa[a[i]][0]; while (b[i]!=u) tmp+=dp2[b[i]]-dp1[b[i]], b[i]=pa[b[i]][0]; dp1[u]=max(dp1[u], tmp+c[i]); //cout<<i<<' '<<tmp<<' '<<c[i]<<'\n'; } //cout<<"value"<<u<<' '<<dp1[u]<<' '<<dp2[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...