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, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const ll inf = 1e18;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 2e5;
int n, d;
vector<int> g[mxn];
int depth[mxn];
void dfs(int u, int p){
for(auto e : g[u]){
if(e == p) continue;
depth[e] = depth[u] + 1;
dfs(e, u);
}
}
bool deleted[mxn];
void _remove(int u, int p, int dis){
deleted[u] = true;
if(dis == d) return;
for(auto e : g[u]){
if(e == p) continue;
_remove(e, u, dis + 1);
}
}
void solve(){
cin >> n >> d;
--d;
fr(i, 0, n-1){
int x;
cin >> x;
g[x].pb(i+1);
g[i+1].pb(x);
}
dfs(0, 0);
vector<int> v;
fr(i, 0, n) v.pb(i);
sort(all(v), [](const int &i, const int &j){
return depth[i] > depth[j];
});
int ans = 0;
for(auto u : v){
if(deleted[u]) continue;
_remove(u, u, 0);
++ans;
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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... |