This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Bismi ALlah
#include "bits/stdc++.h"
using namespace std;
#define int long long
signed main () {
int n, d, cnt = 0;
cin >> n >> d;
vector <int> g[n], depth(n);
function <void(int,int,int,int)> dfs=[&](int v, int p, int opt, int deep) {
depth[v] = deep;
if(deep % d == 0 && opt) cnt ++;
for(auto u : g[v]) {
if(u == p) continue;
dfs(u, v, opt, deep+1);
}
};
for(int i = 1; i < n; i ++) {
int p;
cin >> p;
g[i].push_back(p);
g[p].push_back(i);
}
dfs(0, -1, 0, 0);
int mx = max_element(depth.begin(), depth.end()) - depth.begin();
dfs(mx, -1, 1, 0);
cout << cnt;
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... |