Submission #333869

#TimeUsernameProblemLanguageResultExecution timeMemory
333869limabeansCat in a tree (BOI17_catinatree)C++17
100 / 100
103 ms39276 KiB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;


const int maxn = 1e6 + 5;



int n, d;
vector<int> g[maxn];
int ans = 0;



int dfs(int at) {
    vector<int> w;
    for (int to: g[at]) {
	w.push_back(dfs(to));
    }

    if (w.empty()) {
	ans++;
	return 1;
    }

    sort(w.rbegin(),w.rend());
    int cur = w[0];
    for (int i=1; i<(int)w.size(); i++) {
	if (cur+w[i]<d) {
	    ans--; //remove nearest so far
	} else {
	    cur=w[i];
	}
    }
    if (cur>=d) {
	cur-=d;
	ans++;
    }
    return cur+1;
}


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;
	g[p].push_back(i);
    }


    dfs(0);
    cout<<ans<<endl;    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...