This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define X first
#define Y second
int n;
vector<int>e[200005];
int vis[200005];
int fa[25][200005];
int dis[25][200005];
int dph[200005];
int sbsz[200005];
int lvl[200005];
void dfssz(int nw,int pa){
sbsz[nw]=1;
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i];
if(vis[v]||v==pa){continue;}
dfssz(v,nw);
sbsz[nw]+=sbsz[v];
}
}
int fincen(int nw,int pa,int tot){
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i];
if(vis[v]||v==pa){continue;}
int vl=fincen(v,nw,tot);
if(vl){return vl;}
}
if(sbsz[nw]*2>=tot){
return nw;
}return 0;
}
void dfsdph(int nw,int pa,int flv,int cfa){
dph[nw]=dph[pa]+1;
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i];
if(v==pa||vis[v]){continue;}
dfsdph(v,nw,flv,cfa);
}
dis[flv][nw]=dph[nw]-1;
fa[flv][nw]=cfa;
}
void decompose(int rt,int lv){
dfssz(rt,0);
int cen=fincen(rt,0,sbsz[rt]);
lvl[cen]=lv;
dfsdph(cen,0,lv,cen);
vis[cen]=1;
for(int i=0;i<e[cen].size();i++){
int v=e[cen][i];
if(vis[v]){continue;}
decompose(v,lv+1);
}
}
int mnd[200005];
void upd(int v){
mnd[v]=0;
int nw=v;
for(int i=lvl[v]-1;i>=1;i--){
nw=fa[i][v];
int d=dis[i][v];
mnd[nw]=min(mnd[nw],d);
}
}
int qry(int v){
int ret=mnd[v];
int nw=v;
for(int i=lvl[v]-1;i>=1;i--){
nw=fa[i][v];
int d=dis[i][v];
ret=min(ret,d+mnd[nw]);
}
return ret;
}
int D;
signed main() {
cin>>n>>D;
for(int i=2;i<=n;i++){
int a;int b;
cin>>a;
a++;b=i;
e[a].push_back(b);
e[b].push_back(a);
}
decompose(1,1);
for(int i=1;i<=n;i++){
vis[i]=0;
}
dfsdph(1,0,0,0);
for(int i=1;i<=n;i++){
mnd[i]=1e9;
}
vector<pair<int,int>>vc;
for(int i=1;i<=n;i++){
vc.push_back({-dph[i],i});
}
sort(vc.begin(),vc.end());
int ans=0;
for(int i=0;i<vc.size();i++){
int v=vc[i].second;
if(qry(v)<D){continue;}
ans++;
upd(v);
}
cout<<ans<<endl;
}
Compilation message (stderr)
catinatree.cpp: In function 'void dfssz(int, int)':
catinatree.cpp:18:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
catinatree.cpp: In function 'int fincen(int, int, int)':
catinatree.cpp:26:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
catinatree.cpp: In function 'void dfsdph(int, int, int, int)':
catinatree.cpp:38:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
catinatree.cpp: In function 'void decompose(int, int)':
catinatree.cpp:53:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i=0;i<e[cen].size();i++){
| ~^~~~~~~~~~~~~~
catinatree.cpp: In function 'int main()':
catinatree.cpp:104:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i=0;i<vc.size();i++){
| ~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |