제출 #1280918

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

#define rep(i, a, b) for (int i = a; i < b; i++)

typedef long long ll;

int a;

int n, d;
vector<vector<int>> tree;
vector<bool> removed;
int rem = 0;
int ans = 0;

// bfs
int furthest(int node, int par) {
    int ret = 0;

    for (int kid : tree[node]) {
        if (kid == par) continue;
        if (removed[kid]) continue;
        ret = max(ret, furthest(kid, node) + 1);
    }

    return ret;
}

// diameter
void remove(int node, int di) {
    if (di >= d) return;

    removed[node] = true;
    rem++;

    for (int kid : tree[node]) {
        if (removed[kid]) continue;
        remove(kid, di + 1);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> d;
    tree.assign(n, {});
    removed.assign(n, false);

    rep(i,1,n) {
        cin >> a;
        tree[i].push_back(a);
        tree[a].push_back(i);
    }

    while (rem < n) {
        rep(i,0,n) {
            if (!removed[i])
            {
                a = furthest(i, -1);
                a = furthest(a, -1);

                remove(a, 0);
                ans++;

                break;
            }
        }
    }

    cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...