Submission #491226

#TimeUsernameProblemLanguageResultExecution timeMemory
491226RainbowbunnyCat in a tree (BOI17_catinatree)C++17
100 / 100
145 ms18300 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 5;

int n, d;
int H[MAXN], A[MAXN];
vector <int> Adj[MAXN];

void DFS(int node, int p = -1)
{
    for(auto x : Adj[node])
    {
        if(x == p)
        {
            continue;
        }
        H[x] = H[node] + 1;
        DFS(x, node);
    }
}

void Find(int node)
{
    if(A[node] == 1)
    {
        return;
    }
    for(auto x : Adj[node])
    {
        if(A[x] < A[node] - 1)
        {
            A[x] = A[node] - 1;
            Find(x);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> d;
    for(int i = 1; i < n; i++)
    {
        int p;
        cin >> p;
        Adj[p].push_back(i);
        Adj[i].push_back(p);
    }
    DFS(0);
    vector <int> id;
    for(int i = 0; i < n; i++)
    {
        id.emplace_back(i);
    }
    sort(id.begin(), id.end(), [](int x, int y)
    {
        return H[x] > H[y];
    });
    int ans = 0;
    for(auto x : id)
    {
        if(!A[x])
        {
            A[x] = d;
            ans++;
            Find(x);
        }
    }
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...