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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 5;
const int inf = 1e9;
vector<int> g[N];
int dep[N], mx[N], ans[N], k;
int Dfs(int s, int d) {
if (g[s].size() == 0 && d >= k) {
ans[s] = 1;
return 0;
}
else if (g[s].size() == 0) return inf;
for (int u : g[s]) {
int nesto = Dfs(u, d + 1);
smin(d, nesto + 1);
}
if (d >= k) {
ans[s] = 1;
return 0;
}
return d;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n >> k;
for (int i = 1; i < n; i++) {
int p; cin >> p;
g[p].push_back(i);
dep[i] = dep[p] + 1;
}
for (int i = n - 1; i >= 0; i--) {
mx[i] = dep[i];
sort(g[i].begin(), g[i].end(), [&] (int xx, int yy) {
return mx[xx] > mx[yy];
});
for (int j : g[i]) smax(mx[i], mx[j]);
}
Dfs(0, inf);
int sum = 0;
for (int i = 0; i < n; i++) sum += ans[i];
cout << sum << en;
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... |