제출 #1016581

#제출 시각아이디문제언어결과실행 시간메모리
1016581WhisperCat in a tree (BOI17_catinatree)C++17
100 / 100
50 ms32340 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX         200005

vector<int> G[MAX];
int numNode, maxDist;
int pa[MAX];

pair<int, int> dp[MAX];
int dep[MAX];
void dfs(int u, int p = -1){
    vector<pair<int, int>> store;
    for (int &v : G[u]) if(v ^ p){
        dep[v] = dep[u] + 1;
        dfs(v, u);
        store.push_back(dp[v]);
    }

    sort(store.rbegin(), store.rend());
    int min_depth = INF;
    for (pair<int, int>&x : store){
        int de = x.first, tak = x.second;
        if (de + min_depth - 2 * dep[u] >= maxDist){
            dp[u].second += tak;
            minimize(min_depth, de);
        }
        else{
            dp[u].second += tak - 1;
        }
    }
    if (min_depth - dep[u] >= maxDist) ++dp[u].second, min_depth = dep[u];
    dp[u].first = min_depth;
}
void process(void){
    cin >> numNode >> maxDist;
    for (int i = 2; i <= numNode; ++i){
        cin >> pa[i]; ++pa[i];
        G[pa[i]].emplace_back(i);
        G[i].emplace_back(pa[i]);
    }
    dfs(1);

    cout << dp[1].second;
}

signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);

    process();
}



#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...