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 FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
const int MAXN = (int)(2e5);
bool blocked[MAXN];
int cnt[MAXN];
vector<int> adj[MAXN];
int ans = 0;
vector<int> pos;
void bk(int n, int d) {
if (d == 0) {
cnt[n]--;
if (cnt[n] == 1) {
pos.push_back(n);
}
return;
}
else {
blocked[n] = true;
for (int e : adj[n]) {
if (!blocked[e]) {
bk(e, d-1);
}
}
}
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int N, D;
cin >> N >> D;
FR(i, N-1) {
int p;
cin >> p;
adj[i+1].push_back(p);
cnt[i+1]++;
adj[p].push_back(i+1);
cnt[p]++;
}
FR(i, N) {
if (cnt[i] == 1) {
pos.push_back(i);
}
}
while (pos.size()) {
int nxt = pos.back();
pos.pop_back();
if (!blocked[nxt]) {
ans++;
bk(nxt, D);
}
}
cout << ans << '\n';
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... |