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 ar array
typedef long long ll;
//~ #define int ll
template<class T> bool umin(T& a, const T b) { if(b < a) { a = b; return true; } return false; }
template<class T> bool umax(T& a, const T b) { if(a < b) { a = b; return true; } return false; }
void solve(){
int n, D; cin >> n >> D;
vector<vector<int>> edges(n + 1);
for(int i=2;i<=n;i++){
int a; cin >> a; a++;
edges[a].push_back(i);
edges[i].push_back(a);
}
vector<int> d(n + 1);
function<void(int, int)> dfs = [&](int u, int p){
for(auto x : edges[u]){
if(x == p) continue;
d[x] = d[u] + 1;
dfs(x, u);
}
};
dfs(1, 1);
int a = 1;
for(int i=1;i<=n;i++){
if(d[i] > d[a]) a = i;
}
d[a] = 0;
dfs(a, a);
vector<int> p(n);
iota(p.begin(), p.end(), 1);
sort(p.begin(), p.end(), [&](int i, int j){
return d[i] > d[j];
});
vector<int> vis(n + 1);
int res = 0;
for(auto x : p){
function<void(int, int, int)> dfs = [&](int u, int p, int d_){
if(d_ >= D) return;
vis[u] = 1;
for(auto x : edges[u]){
if(x == p) continue;
dfs(x, u, d_ + 1);
}
};
if(!vis[x]){
res++;
dfs(x, x, 0);
}
}
cout<<res<<"\n";
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
//~ cin >> t;
while(t--){
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... |