Submission #391328

# Submission time Handle Problem Language Result Execution time Memory
391328 2021-04-18T15:10:20 Z Victor Cat in a tree (BOI17_catinatree) C++17
0 / 100
10 ms 14284 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; --i)
#define trav(x, a) for (auto& x : a)
#define sz(a) a.size()
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

vi graph[200001];
int d, x;
map<int, int> memo[200001];

void add(map<int, int>& mp, int dep, int val) {
    vi rem;
    auto it = mp.lower_bound(dep);
    if (val <= it->second) return;
    if (it->first == dep) ++it;

    while (it != mp.begin()) {
        --it;
        if (val < it->second) break;
        rem.push_back(it->first);
    }

    trav(depth, rem) mp.erase(depth);
    mp.emplace(dep, val);
}

void dp(int u, int depth) {
    map<int, int>& curr = memo[u];
    curr.emplace(depth, 1);

    trav(v, graph[u]) {
        dp(v, depth + 1);

        map<int, int>&prev = memo[v], next = curr;
        if (sz(curr) < sz(prev)) swap(next, prev);

        trav(item, prev) {
            int dep, val;
            tie(dep, val) = item;

            auto it = curr.lower_bound(depth * 2 + d - dep);

            if (it != curr.end()) {
                x = u;
                add(next, min(dep, it->first), val + it->second);
            }
        }

        trav(item, prev) {
            int dep, val;
            tie(dep, val) = item;
            add(next, dep, val);
        }

        curr.swap(next);
        memo[v].clear();
    }

}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n, p;
    cin >> n >> d;
    d = min(d, n);

    rep(i, 1, n) {
        cin >> p;
        graph[p].push_back(i);
    }
    
    dp(0, 0);
    cout << memo[0].begin()->second << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 14284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 14284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 14284 KB Output isn't correct
2 Halted 0 ms 0 KB -