/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD1 (1000000000+7)
#define MOD (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;
int n, d, dp[N][2]; // count, near
vector<int> g[N];
void solve(){
cin >> n >> d;
for(int i = 1; i < n; ++i){
int p; cin >> p;
g[p].pb(i);
}
for(int i = 0; i < n; ++i) dp[i][0] = 0, dp[i][1] = MOD;
for(int i = n - 1; i >= 0; --i){
if(g[i].empty()){
dp[i][0] = 1;
dp[i][1] = 0;
}
for(int u: g[i]){
if(dp[i][1] == MOD){
dp[i][0] = dp[u][0];
dp[i][1] = dp[u][1] + 1;
}else{
if(dp[u][1] + 1 + dp[i][1] < d){
dp[i][0] += dp[u][0] - 1;
}else{
dp[i][0] += dp[u][0];
}
dp[i][1] = min(dp[i][1], dp[u][1] + 1);
}
}
if(dp[i][1] == d - 1) dp[i][0]++, dp[i][1] = 0;
}
cout << dp[0][0];
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message
catinatree.cpp: In function 'int main()':
catinatree.cpp:57:16: warning: unused variable 'aa' [-Wunused-variable]
57 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |