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;
const int N = 1e5 + 100;
int n , d , res = 0;
vector <int> a[N];
int high[N], oder[N], value[N], par[N];
void dfs(int u){
for (int v : a[u])
if (v != par[u]){
high[v] = high[u] + 1;
par[v] = u;
dfs(v);
}
}
int get_min(int u){
int val = 1e9 , x = 0;
while (u != 0){
val = min(val,value[u] + x);
u = par[u], x++;
}
return val;
}
int update(int u){
int val = 0; res++;
while (u != 0){
value[u] = min(value[u],val);
u = par[u]; val++;
}
}
bool cmp(int x ,int y){return high[x] > high[y];}
int main(){
//freopen("txt.inp","r",stdin);
// freopen("txt.out","w",stdout);
cin >> n >> d;
for (int i = 2; i <= n ; i++){
int x; cin >> x; x++;
a[x].push_back(i);
a[i].push_back(x);
}
for (int i = 1 ; i < n ; i++){
// int x , y;
// cin >> x >> y;
// a[x].push_back(y);
// a[y].push_back(x);
oder[i] = i;
}
oder[n] = n; par[1] = 0;
dfs(1);
sort(oder + 1,oder + n + 1,cmp);
memset(value,0x3f,sizeof(value));
for (int i = 1 ; i <= n ; i++)
if (get_min(oder[i]) >= d) update(oder[i]);
cout << res;
return 0;
}
Compilation message (stderr)
catinatree.cpp: In function 'int update(int)':
catinatree.cpp:32:1: warning: no return statement in function returning non-void [-Wreturn-type]
32 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |