Submission #391338

#TimeUsernameProblemLanguageResultExecution timeMemory
391338VictorCat in a tree (BOI17_catinatree)C++17
51 / 100
80 ms18368 KiB
#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 (it != mp.end()) { 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]; if (sz(curr) < sz(prev)) swap(curr, prev); auto next = curr; 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...