Submission #932788

#TimeUsernameProblemLanguageResultExecution timeMemory
932788abcvuitunggioBeech Tree (IOI23_beechtree)C++17
100 / 100
1585 ms177004 KiB
#include "beechtree.h" #include <bits/stdc++.h> using namespace std; const int lim=200001,mxn=2e7+1; int n,m,bad[lim],sz[lim],tin[lim],tout[lim],t,id,pos[lim],order[lim],root[lim][2],last[lim],cur,a[lim],b[lim]; int le[mxn],ri[mxn],st[mxn]; vector <int> p,c,res,ke[lim],ve[lim],s[lim],sc[lim],tmp; vector <pair <int, int>> ve2; void dfs(int u){ sz[u]=1; tin[u]=++t; for (int v:ke[u]){ dfs(v); sz[u]+=sz[v]; } tout[u]=t; ve[sz[u]].push_back(u); } int update(int node, int l, int r, int i){ cur++; if (l==r){ st[cur]++; return cur; } int tmp=cur,mid=(l+r)>>1; le[tmp]=le[node]; ri[tmp]=ri[node]; if (mid<i) ri[tmp]=update(ri[node],mid+1,r,i); else le[tmp]=update(le[node],l,mid,i); st[tmp]=st[le[tmp]]+st[ri[tmp]]; return tmp; } int get(int node, int l, int r, int u, int v){ if (l>v||r<u||!node) return 0; if (l>=u&&r<=v) return st[node]; int mid=(l+r)>>1; return get(le[node],l,mid,u,v)+get(ri[node],mid+1,r,u,v); } int get(int i, int j, int k){ int res=get(root[i][k],1,n,tin[j],tout[j])-1; return res-(k&&c[i]==c[j]); } void dfs2(int u){ if (ke[u].empty()){ s[u].push_back(pos[u]); return; } int mx=0,bigchild=-1; for (int v:ke[u]) if (sz[v]>mx){ bigchild=v; mx=sz[v]; } for (int v:ke[u]){ dfs2(v); bad[u]|=bad[v]; } if (bad[u]) return; swap(s[u],s[bigchild]); s[u].push_back(pos[u]); for (int v:ke[u]) if (v!=bigchild){ for (int i:s[v]){ a[order[i]]=get(order[i],u,0); b[order[i]]=get(order[i],u,1); } for (int i:s[v]) if (b[order[i]]!=a[p[order[i]]]){ bad[u]=1; return; } } if (get(bigchild,u,1)){ bad[u]=1; return; } for (int v:ke[u]) if (v!=bigchild){ for (int i:s[v]){ s[u].push_back(i); tmp.push_back(a[order[i]]); sc[c[order[i]]].push_back(b[order[i]]); } s[v].clear(); } sc[c[bigchild]].push_back(get(bigchild,u,1)); tmp.push_back(0); tmp.push_back(sz[u]); sort(tmp.begin(),tmp.end()); int last=-1; ve2.clear(); for (int i:tmp){ int x=i-last-1; if (x) ve2.push_back({x,last}); last=i; } tmp.clear(); int cnt=0; for (int v:ke[u]){ int i=c[v]; int num=get(::last[i],1,n,tin[u],tout[u])-(c[u]==i); int id=0; pair <int, int> cur=ve2[0]; sc[i].push_back(num); sort(sc[i].begin(),sc[i].end()); int last=-1; for (int j:sc[i]){ int x=j-last-1; if (x){ cnt+=x; if (!(x<=cur.first&&last==cur.second)){ bad[u]=1; for (int v:ke[u]) sc[c[v]].clear(); return; } cur.first-=x; if (!cur.first){ id++; cur=(id==ve2.size()?make_pair(0,0):ve2[id]); } } last=j; } sc[i].clear(); } if (cnt<sz[bigchild]-1) bad[u]=1; } vector <int> beechtree(int N, int M, vector<int> P, vector<int> C){ n=N,m=M,p=P,c=C; for (int i=1;i<n;i++) ke[P[i]].push_back(i); dfs(0); for (int i=n;i>=0;i--){ sort(ve[i].begin(),ve[i].end(),[](int u, int v){return pos[p[u]]<pos[p[v]];}); for (int j:ve[i]){ pos[j]=++id; order[id]=j; } } for (int i=1;i<=n;i++){ int j=order[i]; root[j][0]=update(root[order[i-1]][0],1,n,tin[j]); root[j][1]=update(last[c[j]],1,n,tin[j]); last[c[j]]=root[j][1]; } dfs2(0); for (int i=0;i<n;i++) res.push_back(bad[i]^1); return res; }

Compilation message (stderr)

beechtree.cpp: In function 'void dfs2(int)':
beechtree.cpp:126:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |                     cur=(id==ve2.size()?make_pair(0,0):ve2[id]);
      |                          ~~^~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...