제출 #730393

#제출 시각아이디문제언어결과실행 시간메모리
730393USERCat in a tree (BOI17_catinatree)C++14
0 / 100
2 ms4996 KiB
#include <bits/stdc++.h>
using namespace std;

int n, k, lvl[200001];
vector <int> g[200001];

pair <int, int> dfs(int nod = 0, int height = 0) {

    lvl[nod] = height;
    vector <pair<int, int>> v;
    int ret = 1;
    v.push_back({height, nod});

    for (auto &i : g[nod]) {

        pair <int, int> aux = dfs(i, height + 1);
        v.push_back({lvl[aux.first], aux.first});
        ret += aux.second;
    }

    sort(v.begin(), v.end());

    int i = 0;
    while (v.size() - i > 1 && v[i].first + v[i + 1].first < k)
        i++, ret--;

    return {v[i].second, ret};
}

int main() {

    cin >> n >> k;

    for (int i = 1; i < n; i++) {

        int x;
        cin >> x;

        g[x].push_back(i);
    }

    cout << dfs().second;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...