Submission #732928

#TimeUsernameProblemLanguageResultExecution timeMemory
732928onepunchac168Election Campaign (JOI15_election_campaign)C++14
100 / 100
282 ms47384 KiB
// created by Dinh Manh Hung // onepunchac168 // THPT CHUYEN HA TINH // HA TINH, VIET NAM #include <bits/stdc++.h> using namespace std; //#pragma GCC optimize("O3,unroll-loops,no-stack-protector") //#pragma GCC target("sse4,avx2,fma") #define task "" #define ldb long double #define pb push_back #define fi first #define se second #define pc pop_back() #define all(x) begin(x),end(x) #define uniquev(v) v.resize(unique(all(v))-v.begin()) #define FOR(i,a,b) for (int i=a;i<=b;i++) #define cntbit(v) __builtin_popcountll(v) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) ((1LL*a*b)/__gcd(a,b)) #define mask(x) (1LL<<(x)) #define readbit(x,i) ((x>>i)&1) #define ins insert typedef long long ll; typedef pair <ll,ll> ii; typedef pair <ll,ii> iii; typedef pair <ii,ii> iiii; ll dx[]= {1,-1,0,0,1,-1,1,-1}; ll dy[]= {0,0,-1,1,1,-1,-1,1}; const ldb PI = acos (-1); //const ll mod=978846151; //const ll base=29; const int maxn=1e6+5; const int mod=1e9+7; const char nl = '\n'; inline int ReadInt() { char co; for (co = getchar(); co < '0' || co > '9'; co = getchar()); int xo = co - '0'; for (co = getchar(); co >= '0' && co <= '9'; co = getchar()) xo = xo * 10 + co - '0'; return xo; } void WriteInt(int xo) { if (xo > 9) WriteInt(xo / 10); putchar(xo % 10 + '0'); } /* END OF TEMPLATE*/ // ================= Solution =================// const int N=1e5+5; ll n,m; vector <ll> vt[N]; vector <iii> tmp[N]; ll parent[N][20]; ll sz[N]; ll dp[N]; ll in[N]; ll out[N]; ll root=0; ll bit[N]; void update(int u,ll val) { while (u<=n) { bit[u]+=val; u+=u&(-u); } } ll get(int u) { ll suma=0; while (u>0) { suma+=bit[u]; u-=u&(-u); } return suma; } void dfsa(int u,int vv) { in[u]=++root; for (auto v:vt[u]) { if (v==vv) { continue; } parent[v][0]=u; for (int i=1;i<=18;i++) { parent[v][i]=parent[parent[v][i-1]][i-1]; } sz[v]=sz[u]+1; dfsa(v,u); } out[u]=root; } ll par[N]; ll findpar(int u) { if (par[u]<0) { return u; } return par[u]=findpar(par[u]); } void dfs(int u,int vv) { ll sum=0; for (auto v:vt[u]) { if (v==vv) { continue; } dfs(v,u); dp[u]=max(dp[u],dp[v]); sum+=dp[v]; } ll gmax=0; for (auto v:tmp[u]) { ll ua=v.se.fi; ll va=v.se.se; ll wa=v.fi; ll indexa=get(in[ua])+get(in[va]); ll indexb=sum; ll a1=findpar(ua); ll a2=findpar(va); if (a1!=u) { indexb-=dp[a1]; } if (a2!=u) { indexb-=dp[a2]; } gmax=max(gmax,indexa+indexb+wa); } dp[u]=max({dp[u],gmax,sum}); update(in[u],sum); update(in[u]+1,-sum); for (auto v:vt[u]) { if (v==vv) { continue; } update(in[v],sum-dp[v]); update(out[v]+1,-sum+dp[v]); par[v]=u; } } ll lca(int u,int v) { if (sz[u]>sz[v]) { swap(u,v); } for (int i=18;i>=0;i--) { if (sz[v]-mask(i)>=sz[u]) { v=parent[v][i]; } } if (u==v){return u;} for (int i=18;i>=0;i--) { if (parent[u][i]!=parent[v][i]) { u=parent[u][i]; v=parent[v][i]; } } if (u!=v) { u=parent[u][0]; v=parent[v][0]; } return u; } void optmushnpr() { cin>>n; for (int i=1;i<=n;i++) { par[i]=-1; } for (int i=1;i<n;i++) { ll u,v; cin>>u>>v; vt[u].pb(v); vt[v].pb(u); } dfsa(1,-1); cin>>m; for (int i=1;i<=m;i++) { ll a,b,c; cin>>a>>b>>c; ll pp=lca(a,b); tmp[pp].pb({c,{a,b}}); } dfs(1,-1); cout<<dp[1]; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); if (fopen(task".inp","r")){ freopen(task".inp","r",stdin); freopen(task".out","w",stdout);} int t; t=1; //cin>>t; while (t--){optmushnpr();} } // goodbye see ya

Compilation message (stderr)

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:225:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  225 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:226:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  226 |     freopen(task".out","w",stdout);}
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...