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 kN = 2e5;
vector<int> g[kN];
int mark[kN];
void dfs(int u, int d) {
mark[u] = d;
for (int v : g[u]) {
if (mark[v] < d - 1) {
dfs(v, d - 1);
}
}
}
void testCase() {
int n, d;
cin >> n >> d;
vector<int> dep(n);
vector<pair<int, int>> nodes(n, {0, 0});
for (int v = 1; v < n; ++v) {
int u;
cin >> u;
g[u].emplace_back(v);
g[v].emplace_back(u);
dep[v] = dep[u] + 1;
nodes[v] = {dep[v], v};
}
sort(nodes.begin(), nodes.end(), greater<pair<int, int>>());
int ans = 0;
for (auto it : nodes) {
if (mark[it.second] == 0) {
ans += 1;
dfs(it.second, d);
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
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... |