#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
int a;
int n, d;
vector<vector<int>> tree;
vector<bool> removed;
int rem = 0;
int ans = 0;
// bfs
int furthest(int node, int par) {
int ret = 0;
for (int kid : tree[node]) {
if (kid == par) continue;
if (removed[kid]) continue;
ret = max(ret, furthest(kid, node) + 1);
}
return ret;
}
// diameter
void remove(int node, int di) {
if (di >= d) return;
removed[node] = true;
rem++;
for (int kid : tree[node]) {
if (removed[kid]) continue;
remove(kid, di + 1);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> d;
tree.assign(n, {});
removed.assign(n, false);
rep(i,1,n) {
cin >> a;
tree[i].push_back(a);
tree[a].push_back(i);
}
while (rem < n) {
rep(i,0,n) {
if (!removed[i])
{
a = furthest(i, -1);
a = furthest(a, -1);
remove(a, 0);
ans++;
break;
}
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |