// @EXPECTED_RESULTS@: TIME_LIMIT_EXCEEDED, WRONG_ANSWER #include using namespace std; const int oo = 0x3f3f3f3f; const double eps = 1e-9; const double PI = 2.0 * acos(0.0); typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef vector vi; typedef vector vs; #define sz(c) int((c).size()) #define all(c) (c).begin(), (c).end() #define FOR(i,a,b) for (int i = (a); i < (b); i++) #define FORS(i,a,b,s) for (int i = (a); i < (b); i=i+(s)) #define FORD(i,a,b) for (int i = int(b)-1; i >= (a); i--) #define FORIT(i,c) for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++) int pre[1000*1000]; int nc[1000*1000]; int branch_depth(int i, int & bn){ if (i == 0) {bn = i; return 0;} if (nc[i] > 1) {bn = i; return 0;} return 1 + branch_depth(pre[i], bn); } int main(){ //cout << 1 << endl; //return 0; int N,K; cin >> N >> K; set nextRound; FOR(i,1,N){ cin >> pre[i]; nc[pre[i]]++; } FOR(i,0,N){ if (nc[i]) continue; // only do this for leafs nextRound.insert(i); } int R = nextRound.size(); while (K){ set> toTake; FOR(i,1,N){ if (nc[i]) continue; // only do this for leafs int bn; int d = branch_depth(i,bn); //cout << i << " " << d << " " << bn << endl; toTake.insert({d,{i,bn}}); } bool taken = false; FORIT(t,toTake){ if (nc[t->second.second] == 1) continue; // already taken all neighbours if (t->first > K){ cout << R << endl; return 0; } K-=t->first; R--; nc[t->second.second]--; nextRound.erase(t->second.first); taken = true; //cout << t->first << " " << t->second.first << " " << t->second.second << endl; } if (! taken) break; } cout << R << endl; return 0; }