Submission #703139

#TimeUsernameProblemLanguageResultExecution timeMemory
703139browntoadCat in a tree (BOI17_catinatree)C++14
100 / 100
161 ms36720 KiB
#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
multiset<int> ::iterator it, it2;
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){
        it=pen[x].begin();
        it2=next(it);
        if ((*it)-dep+(*it2)-dep < d){
            pen[x].erase(it);
        }
        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)

catinatree.cpp: In function 'void dfs(int, int)':
catinatree.cpp:10:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |     for (int i=0; i<graph[x].size(); i++){
      |                   ~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...