Submission #650445

# Submission time Handle Problem Language Result Execution time Memory
650445 2022-10-13T18:36:02 Z Lobo Cat in a tree (BOI17_catinatree) C++17
0 / 100
2 ms 4948 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 2e5+10;

int n,D, dpq[maxn], dp1[maxn], dp2[maxn];
vector<int> g[maxn];

// quantity of marked nodes
// distance from the closest marked node
// distance from the closest marked node more than (D+1)/2

void dfs(int u) {
    dpq[u] = 0;
    dp1[u] = 0;
    dp2[u] = D+1;
    if(g[u].size() == 0) return;

    for(auto v : g[u]) {
        dfs(v);
        dpq[u]+= dpq[v];

        dp2[u] = min(dp2[u],dp2[v]+1);
        if(dp1[v]+1 >= (D+1)/2) {
            dpq[u]++;
            dp2[u] = min(dp2[u],dp1[v]+1);
        }
        else {
            dp1[u] = max(dp1[u],dp1[v]+1);
        }
    }

    if(dp1[u]+dp2[u] < D) {
        dp1[u] = 0;
    }

    if(u == 1 && dp1[u]+dp2[u] >= D) {
        dpq[u]++;
    }
}

void solve() {
    cin >> n >> D;
    for(int i = 2; i <= n; i++) {
        int x; cin >> x;
        g[x+1].pb(i);
    }

    dfs(1);
    cout << dpq[1] << endl;

}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -