Submission #1107029

#TimeUsernameProblemLanguageResultExecution timeMemory
1107029koukirocksCat in a tree (BOI17_catinatree)C++17
100 / 100
47 ms19296 KiB
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("popcnt")
 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
const ll MAX=2e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const ldb eps=1e-6;
const ldb PI=acos(-1.0);
const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
template<typename T>
using vvector = vector<vector<T>>;

pii dfs(int v,int p,vvector<int> &G,int k) {
    int ans=0,dep=INF;
    for (int i:G[v]) {
        if (i==p) continue;
        auto [now,nd]=dfs(i,v,G,k);
        if (nd+dep>=k) {
            ans+=now;
            dep = min(dep,nd);
        } else {
            ans+=now-1;
            dep=max(dep,nd);
        }
    }
    if (dep==INF or dep>=k) {
        ans++;
        dep=0;
    }
    // cout<<v<<" "<<ans<<" "<<dep<<" v ans dep\n";
    return {ans,dep+1};
}

int main() {
    speed;
    int n,d;
    cin>>n>>d;
    vvector<int> G(n);
    for (int i=1;i<n;i++) {
        int x;
        cin>>x;
        G[x].push_back(i);
        G[i].push_back(x);
    }
    auto [ans,dp] = dfs(0,-1,G,d);

    cout<<ans<<"\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...