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;
#define pb push_back
#define ff first
#define ss second
#define sz size()
const int N = 1e5 + 5;
int mark[N], n, d, x, vis[N];
vector <int> v[N];
void solve (int x, int pr, int dis) {
if (dis >= d) {
mark[x] = 1;
dis = 0;
}
for (int i : v[x]) {
if (i == pr) continue;
solve (i, x, dis + 1);
}
}
int dis[N];
int main () {
// freopen ("input.txt", "r", stdin);
cin >> n >> d;
for (int i = 1; i < n; ++i) {
int x;
cin >> x;
v[x].pb (i);
v[i].pb (x);
}
queue <int> q;
q.push (0);
vis[0] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i : v[x]) {
if (vis[i]) continue;
q.push (i);
dis[i] = dis[x] + 1;
vis[i] = 1;
}
}
int node = 0;
for (int i = 0; i < n; ++i) {
if (dis[node] < dis[i]) {
node = i;
}
}
solve (node, -1, d);
int ret = 0;
for (int i = 0; i < n; ++i) {
ret += mark[i] == 1;
}
cout << ret;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |