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;
vector<vector<int>> gra;
queue<int> Q;
vector<bool> vis;
int root = 0, n, d, ans = 0;
int dfs(int x, int p){
vector<int> und;
for(int i : gra[x]){
if(i == p)continue;
und.push_back(dfs(i,x));
}
if(!und.size()){ans++;return 1;}
sort(und.rbegin(),und.rend());
int cur = und[0];
for(int i = 1; i < (int)und.size(); ++i){
if(und[0] + und[i] < d)ans--;
else cur = und[i];
}
if(cur >= d){
cur-=d;
ans++;
}
return cur+1;
}
int main(){
cin >> n >> d;
vis.resize(n,false);
gra.resize(n);
for(int i = 1; i < n; ++i){
int a; cin >> a;
gra[a].push_back(i);
gra[i].push_back(a);
}
dfs(0,0);
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |