제출 #1096136

#제출 시각아이디문제언어결과실행 시간메모리
1096136anmattroiCat in a tree (BOI17_catinatree)C++14
100 / 100
44 ms25948 KiB
#include <bits/stdc++.h>

#define maxn 200005

using namespace std;

int n, d;
vector<int> adj[maxn];
int f[maxn];
int ds = 0;

void dfs(int u, int dad) {
    f[u] = INT_MAX/3;
    vector<int> nho;
    for (int v : adj[u]) {
        if (v == dad) continue;
        dfs(v, u);
        nho.emplace_back(f[v]+1);
    }
    sort(nho.begin(), nho.end());
    reverse(nho.begin(), nho.end());
    while (nho.size() >= 2 && nho[int(nho.size())-1] + nho[int(nho.size())-2] < d) {--ds; nho.pop_back();}
    if (!nho.empty()) f[u] = nho.back();
    if (f[u] >= d) {
        ++ds;
        f[u] = 0;
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> d;
    for (int i = 2; i <= n; i++) {
        int j; cin >> j; ++j;
        adj[i].emplace_back(j);
        adj[j].emplace_back(i);
    }
    dfs(1, 0);
    cout << ds;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...