This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Tr3nity */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod1 (1000000000+7)
#define mod (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp make_pair
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 18;
struct dp{
int val, dist;
};
int n, d;
vector<int> g[N];
dp a[N];
void update(int v, int x, int dep){
if(x > a[v].val){
a[v].val = x;
a[v].dist = dep;
}else if(x == a[v].val && dep > a[v].dist){
a[v].dist = dep;
}
}
void dfs(int v){
int sum = 0, small = 0, smallest_big = d, biggest_small = -1;
for(int u: g[v]){
dfs(u);
sum += a[u].val;
if(a[u].dist < (d+1)/2){
small++;
if(biggest_small == -1 || a[biggest_small].dist < a[u].dist){
biggest_small = u;
}
}else if(d % 2 == 0 && a[u].dist == d / 2){
biggest_small = u;
}else{
smallest_big = min(smallest_big, a[u].dist);
}
}
if(g[v].size() == 0){
a[v].val = 1, a[v].dist = 0;
return;
}
update(v, sum - small + 1, smallest_big);
if(biggest_small != -1)
update(v, sum - small + 1, a[smallest_big].dist);
a[v].dist++;
}
void solve(){
cin >> n >> d;
for(int i = 1; i <= n-1; ++i){
int x; cin >> x;
g[x].pb(i);
}
for(int i = 0; i < n; ++i) a[i].val = 0, a[i].dist = d;
dfs(0);
cout << a[0].val;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// cin >> tt;aa=tt;
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
catinatree.cpp: In function 'int main()':
catinatree.cpp:71:15: warning: unused variable 'aa' [-Wunused-variable]
71 | int tt = 1, aa;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |