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 ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e5+5;
const int inf=1e9;
int d;
int level[mxn];
set<pii> st;
vector<int> adj[mxn];
void dfs(int cur,int prev,int dist) {
if(dist >= d) return;
if(st.find({level[cur],cur}) != st.end()) st.erase({level[cur],cur});
for(int u : adj[cur]) {
if(u == prev) continue;
dfs(u,cur,dist + 1);
}
}
void DFS(int cur,int prev) {
level[cur] = level[prev] + 1;
for(int u : adj[cur]) {
if(u != prev) DFS(u,cur);
}
}
int main() {
int n;
cin>>n;
cin>>d;
for(int i = 1; i < n; i++) {
int u;
cin>>u;
adj[u].push_back(i);
adj[i].push_back(u);
}
level[0] = 0;
DFS(0,0);
for(int i = 0; i < n; i++) st.insert({level[i],i});
int res = 0;
while(!st.empty()) {
auto it = st.rbegin();
int u = it->second;
res++;
dfs(u,-1,0);
}
cout<<res<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |