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;
typedef long long ll;
const int mxN = 2e5+10;
vector<int> adj[mxN];
int cnt[mxN];
int N, D;
void bfs(int st) {
vector<bool> vis(N+10, 0);
queue<pair<int,int>> q;
q.push({0, st});
vis[st] = 1;
while(q.size()) {
auto node = q.front();
q.pop();
cnt[node.first]++;
for (auto it : adj[node.second]) {
if (!vis[it]) {
vis[it] = 1;
q.push({node.first+1, it});
}
}
}
}
void solve() {
cin >> N >> D;
for (int i = 1; i < N; i++) {
int v;
cin >> v;
adj[i].push_back(v);
adj[v].push_back(i);
}
for (int i = 0; i < N; i++) {
if (adj[i].size() == 1) {
bfs(i);
break;
}
}
int ans = 0;
for (int i = 0; i <= N; i+=D)
ans += cnt[i];
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |