Submission #700064

#TimeUsernameProblemLanguageResultExecution timeMemory
700064tamthegodCat in a tree (BOI17_catinatree)C++17
51 / 100
1077 ms35292 KiB
// 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...