This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n,d,root;
vector<int> con[200005];
int parent[200005],depth[200005],injos[200005];
void dfs(int nod)
{
for(int adj:con[nod])
{
if(adj==parent[nod])
continue;
parent[adj] = nod;
depth[adj] = depth[nod]+1;
dfs(adj);
}
}
bool verif(int nod)
{
if(injos[nod]<d)
return 0;
int cur=nod,auxd=d;
while(cur!=root)
{
cur = parent[cur];
auxd--;
if(injos[cur]<auxd)
return 0;
}
return 1;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>d;
int u;
for(int i=2;i<=n;i++)
{
cin>>u;
u++;
con[i].push_back(u);
con[u].push_back(i);
}
root=1;
for(int i=1;i<=n;i++)
if((int)con[i].size()>1)
root=i;
//cerr<<root<<" root\n";
dfs(root);
vector<pair<int,int>> v(n);
for(int i=1;i<=n;i++)
{
v[i-1] = {-depth[i],i};
injos[i] = d+5;
//cerr<<i<<" "<<parent[i]<<" parent\n";
}
sort(v.begin(),v.end());
int cnt=0;
for(auto [_,i]:v)
{
if(verif(i))
{
injos[parent[i]] = 1;
cnt++;
//cerr<<i<<" ales\n";
}
else
{
injos[parent[i]] = min(injos[parent[i]], injos[i]+1);
}
if(i!=root)
{
int cur = parent[i];
while(cur!=root)
{
injos[parent[cur]] = min(injos[parent[cur]], injos[cur]+1);
cur = parent[cur];
}
}
}
cout<<cnt;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |