# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
703143 | browntoad | Cat in a tree (BOI17_catinatree) | C++14 | 151 ms | 35512 KiB |
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;
const int maxn = 2e5+5;
int n, d;
vector<int> graph[maxn], dp(maxn);
multiset<int> pen[maxn]; // pending
void dfs(int x, int dep){
for (int i=0; i<graph[x].size(); i++){
dfs(graph[x][i], dep+1);
dp[x]+=dp[graph[x][i]];
if (pen[graph[x][i]].size() > pen[x].size()) swap(pen[x], pen[graph[x][i]]);
for (int j:pen[graph[x][i]]) pen[x].insert(j);
}
while(pen[x].size()>=1 && (*prev(pen[x].end()))>=dep+d) {
dp[x]++;
pen[x].erase(prev(pen[x].end()));
}
while(pen[x].size()>=2){
if ((*pen[x].begin())-dep+(*next(pen[x].begin()))-dep < d){
pen[x].erase(pen[x].begin());
}
else break;
}
if (pen[x].size()==0) pen[x].insert(dep);
}
int main(){
cin>>n>>d;
for (int i=2; i<=n; i++){
int x; cin>>x;
graph[x+1].push_back(i);
}
dfs(1, 0);
cout<<dp[1]+pen[1].size()<<endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |