Submission #25424

#TimeUsernameProblemLanguageResultExecution timeMemory
25424tlwpdusElection Campaign (JOI15_election_campaign)C++14
100 / 100
486 ms42288 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> pii; struct plan { int a, b, c; plan(int a, int b, int c):a(a),b(b),c(c){} }; int n, m; vector<int> lis[100100]; vector<plan> pl[100100]; int sz[100100]; map<int,int> *E[100100]; int D[100100]; int bias[100100]; int sps[100100][20]; int dep[100100]; void idfs(int here, int p, int d) { int i; sps[here][0] = p; dep[here] = d; for (i=0;i<lis[here].size();i++) { int there = lis[here][i]; if (there==p) continue; idfs(there,here,d+1); } } void init() { int i, j; for (j=1;j<20;j++) { for (i=0;i<n;i++) sps[i][j] = ((~sps[i][j-1])?sps[sps[i][j-1]][j-1]:-1); } } int lca(int u, int v) { if (dep[u]>dep[v]) swap(u,v); int td = dep[v]-dep[u]; int i; for (i=0;i<20;i++) { if ((td>>i)&1) v = sps[v][i]; } if (u==v) return u; for (i=19;i>=0;i--) { if (sps[u][i]==sps[v][i]) continue; u=sps[u][i];v=sps[v][i]; } return sps[u][0]; } void dfs(int here, int p) { int i, maxi = -1, t = -1, dsum = 0; sz[here] = 1; for (i=0;i<lis[here].size();i++) { int there = lis[here][i]; if (there==p) continue; dfs(there,here); if (maxi<sz[there]) { maxi=sz[there]; t=there; } dsum += D[there]; sz[here]+=sz[there]; } if (~t) { E[here] = E[t]; bias[here] = bias[t]+dsum-D[t]; } else { E[here] = new map<int,int>(); E[here]->insert(pii(here,0)); } for (i=0;i<lis[here].size();i++) { int there = lis[here][i]; if (there==p||there==t) continue; map<int,int>::iterator it = E[there]->begin(); while(it!=E[there]->end()) { E[here]->insert(pii((*it).first,(*it).second+bias[there]-bias[here]+dsum-D[there])); it++; } E[there]->clear(); } E[here]->insert(pii(here,dsum-bias[here])); D[here] = dsum; for (i=0;i<pl[here].size();i++) { D[here] = max(D[here],pl[here][i].c+(*E[here])[pl[here][i].a]+bias[here]+(*E[here])[pl[here][i].b]+bias[here]-dsum); } } int main() { int i; scanf("%d",&n); for (i=0;i<n-1;i++) { int x, y; scanf("%d%d",&x,&y); x--;y--; lis[x].push_back(y); lis[y].push_back(x); } idfs(0,-1,0); init(); scanf("%d",&m); for (i=0;i<m;i++) { int a, b, c; scanf("%d%d%d",&a,&b,&c); a--;b--; pl[lca(a,b)].push_back(plan(a,b,c)); } dfs(0,-1); printf("%d\n",D[0]); return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'void idfs(int, int, int)':
election_campaign.cpp:26:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<lis[here].size();i++) {
               ^
election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:58:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<lis[here].size();i++) {
               ^
election_campaign.cpp:77:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<lis[here].size();i++) {
               ^
election_campaign.cpp:89:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<pl[here].size();i++) {
               ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:97:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
                   ^
election_campaign.cpp:100:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&x,&y);
                            ^
election_campaign.cpp:107:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&m);
                   ^
election_campaign.cpp:110:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d",&a,&b,&c);
                                 ^
#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...