Submission #474008

#TimeUsernameProblemLanguageResultExecution timeMemory
474008julian33Cat in a tree (BOI17_catinatree)C++14
51 / 100
1083 ms16076 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
    cerr<<vars<<" = ";
    string delim="";
    (...,(cerr<<delim<<values,delim=", "));
    cerr<<"\n";
}
#else
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {}
#endif

#define pb push_back
#define sz(x) (int)(x.size())
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> inline void maxa(T& a,T b){a=max(a,b);}
template<typename T> inline void mina(T& a,T b){a=min(a,b);} 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mxN=2e5+5; //make sure this is right
const int mod=1e9+7;

vector<int> graph[mxN],path;
int bad[mxN],ht[mxN],seen[mxN];
priority_queue<pii> q;

void dfs(int at,int p=-1){
    for(int &i:graph[at]){
        if(i==p)
            continue;
        ht[i]=ht[at]+1;
        dfs(i,at);
    }
    q.push({ht[at],at});
}

void paint(int at,int d,int dep){
    if(d==0 || ht[at]>dep || seen[at])
        return;
    bad[at]=1;
    seen[at]=1;
    path.pb(at);
    for(int &i:graph[at])
        paint(i,d-1,dep);
}

int main(){
    cin.sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #ifdef LOCAL
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int n,d; cin>>n>>d;
    for(int i=1;i<n;i++){
        int x; cin>>x;
        graph[x].pb(i);
        graph[i].pb(x);
    }
    dfs(0);
    int ans=0;
    while(!q.empty()){
        int at=q.top().second; q.pop();
        if(bad[at])
            continue;
        ans++;
        paint(at,d,ht[at]);
        while(sz(path)){
            seen[path.back()]=0;
            path.pop_back();
        }
    }
    cout<<ans<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...