This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n, m, mini, maxi, medi, t;
vector<int> adj[100005];
int yeets(int x, int k){
if(adj[x].size() == 0) return 1;
priority_queue<int> cs;
for(auto it : adj[x]) cs.push(yeets(it, k));
int t = 0;
for(int i = 1; i <= k; i++){
if(cs.size() == 0) break;
t += cs.top(); cs.pop();
}
return t+1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
if(m == 1){ cout << 0; return 0; }
for(int i = 2; i <= n; i++){ cin >> t; adj[t].push_back(i); }
mini = 1; maxi = n;
while(mini < maxi){
medi = mini+(maxi-mini)/2;
if(yeets(1, medi) >= m) maxi = medi;
else mini = medi+1;
}
cout << mini;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |