Submission #244589

#TimeUsernameProblemLanguageResultExecution timeMemory
244589kshitij_sodaniCat in a tree (BOI17_catinatree)C++17
100 / 100
964 ms82396 KiB
#include <bits/stdc++.h> using namespace std; typedef int64_t llo; #define mp make_pair #define pb push_back #define a first #define b second //#define endl '\n' int n,d; int ans=0; set<int> adj[200001]; int vis[200001]; vector<pair<int,int>> cur; int par[200001]; void dfs(int no,int par2=-1,int levv=0){ cur.pb({levv,no}); for(auto j:adj[no]){ if(j==par2){ continue; } dfs(j,no,levv+1); } } int ss[200001]; void dfs2(int no,int par2=-1,int levv=0){ ss[no]=1; for(auto j:adj[no]){ if(j==par2){ continue; } dfs2(j,no,levv+1); ss[no]+=ss[j]; } } int lo=0; int find(int no,int par2=-1){ for(auto j:adj[no]){ if(j==par2){ continue; } if(ss[j]>lo/2){ return find(j,no); } } return no; } /*vector<int> mm; void dfs2(int no,int par=-1,int levv=0){ vis[no]=1; mm.pb(no); // if(levv<d){ for(auto j:adj[no]){ if(j.b==par){ continue; } if(j.a+levv>=d){ break; } dfs2(j.b,no,levv+j.a); } // } }*/ int cot; int cot2; vector<pair<int,int>> dist[200001]; int dist2[200001][20]; int lev[200001]; int ind[200001]; void calc(int no,int par2=-1,int levv=0){ dist2[no][cot2]=levv; dist[cot].pb({levv,no}); for(auto j:adj[no]){ if(j==par2){ continue; } calc(j,no,levv+1); } } void dec(int no,int par2=-1,int levv=0){ if(levv>=20){ while(true){ continue; } } dfs2(no); lo=ss[no]; int cen=find(no); //cout<<cen<<","<<levv<<endl; cot=cen; cot2=levv; lev[cen]=levv; par[cen]=par2; calc(cen); sort(dist[cen].begin(),dist[cen].end()); ind[cen]=0; for(auto j:adj[cen]){ adj[j].erase(cen); } for(auto j:adj[cen]){ dec(j,cen,levv+1); } adj[cen].clear(); } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n>>d; for(int i=1;i<n;i++){ int aa; cin>>aa; adj[aa].insert(i); adj[i].insert(aa); } dfs(0); sort(cur.begin(),cur.end()); reverse(cur.begin(),cur.end()); dec(0); for(auto no:cur){ if(vis[no.b]){ continue; } int nn=no.b; while(nn!=-1){ int x=dist2[no.b][lev[nn]]; while(ind[nn]<dist[nn].size()){ if(dist[nn][ind[nn]].a+x<d){ vis[dist[nn][ind[nn]].b]=1; ind[nn]+=1; } else{ break; } } nn=par[nn]; } ans+=1; /*ans+=1; dfs2(no.b); for(auto j:mm){ for(auto ll:adj[j]){ if(ll==) if(adj[ll].find(j)!=adj[ll].end()){ adj[ll].erase(j); } } } mm.clear();*/ } cout<<ans<<endl; return 0; }

Compilation message (stderr)

catinatree.cpp: In function 'int main()':
catinatree.cpp:137:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while(ind[nn]<dist[nn].size()){
          ~~~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...