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;
const int N = 200200;
int n, D;
int p[N];
int d[N];
vector<int> g[N];
bool us[N];
int dst[N];
queue<int> q;
void del(int v) {
vector<int> vis(n);
q.push(v);
vis[v] = true;
dst[v] = 0;
while (!q.empty()) {
v = q.front();
us[v] = true;
q.pop();
if (dst[v] == D - 1) continue;
for (int to : g[v]) {
if (!vis[to]) {
vis[to] = true;
dst[to] = dst[v] + 1;
q.push(to);
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> D;
for (int i = 1; i < n; i++) {
cin >> p[i];
g[p[i]].push_back(i);
g[i].push_back(p[i]);
}
for (int i = 1; i < n; i++) {
d[i] = d[p[i]] + 1;
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int a, int b) { return d[a] > d[b]; });
int res = 0;
for (int c : ord) {
if (us[c]) continue;
res++;
del(c);
}
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |