#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> ti;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ld,ld> pld;
#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front
#define ff first
#define ss second
#define MOD (ll)(1000000007)
#define INF (ll) (1e18)
#define all(v) (v).begin(),(v).end()
const int nx[8] = {0, 0, 1, -1,1,1,-1,-1}, ny[8] = {1, -1, 0, 0,1,-1,1,-1}; //East, West, South, North+
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;}
ll lcm(ll a, ll b){return (a / gcd(a, b)) * b;}
////////////******SOLUTION******\\\\\\\\\\\
const int MAX_N = 2e5 +4;
int n, d;
int par[MAX_N];
vi adj[MAX_N];
int depth[MAX_N];
void f(int i, int d)
{
depth[i] = d;
for(auto e: adj[i])
{
if(e == par[i]) continue;
f(e,d+1);
}
}
void solve()
{
cin >> n >> d;
for(int i = 1; i < n; i ++)
{
cin >> par[i];
adj[i].pb(par[i]);
adj[par[i]].pb(i);
}
f(0,0);
int maxt = 0;
int ans = 1;
map<int,vi> m;
for(int i = 0; i <n; i++)
{
m[depth[i]].pb(i);
maxt = max(maxt,depth[i]);
}
for(auto e: m[maxt])
{
int nb = 0;
queue<pii> q;
q.push({e,d});
bool visited[n];
memset(visited,false,sizeof(visited));
while(!q.empty())
{
pii u = q.front();
q.pop();
int node = u.ff;
visited[node] = true;
int y = u.ss + 1;
if(y == d+1)
{
nb ++;
y = 1;
}
for(auto e: adj[node])
{
if(visited[e]) continue;
q.push({e,y});
}
}
ans = max(ans,nb);
}
cout << ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int tt =1;
while(tt--)
{
solve();
}
}
/*
*/
Compilation message
catinatree.cpp:24:1: warning: multi-line comment [-Wcomment]
24 | ////////////******SOLUTION******\\\\\\\\\\\
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |