This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Make the best become better
// No room for laziness
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, d;
vector<int> adj[maxN];
int mark[maxN];
int f[maxN];
int depth[maxN];
void ReadInput()
{
cin >> n >> d;
for(int i=2; i<=n; i++)
{
int p;
cin >> p;
p++;
adj[p].pb(i);
adj[i].pb(p);
}
}
void dfs(int u, int par)
{
for(int v : adj[u])
{
if(v == par) continue;
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
void bfs()
{
queue<int> q;
fill(f, f + n + 1, oo);
for(int i=1; i<=n; i++)
{
if(mark[i])
{
f[i] = 0;
q.push(i);
}
}
while(!q.empty())
{
int u = q.front();
q.pop();
for(int v : adj[u])
{
if(f[v] > f[u] + 1)
{
f[v] = f[u] + 1;
q.push(v);
}
}
}
}
void Solve()
{
dfs(1, 0);
int res = 0;
while(true)
{
bfs();
int id = -1;
for(int i=1; i<=n; i++)
{
if(f[i] < d) continue;
if(id == -1 || depth[id] < depth[i]) id = i;
}
if(id == -1) break;
res++;
mark[id] = 1;
}
cout << res;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |