Submission #1106122

#TimeUsernameProblemLanguageResultExecution timeMemory
1106122ortsacCat in a tree (BOI17_catinatree)C++17
0 / 100
2 ms5112 KiB
#include <bits/stdc++.h> using namespace std; #define pii pair<int, int> #define fr first #define se second const int MAXN = 2e5 + 10; vector<int> adj[MAXN]; int n, d; pii calc(int node) { if (adj[node].empty()) { return {0, 1}; } vector<pii> dec; for (auto u : adj[node]) { dec.push_back(calc(u)); } sort(dec.begin(), dec.end(), greater<pii>()); int lst = 0x3f3f3f3f, ans = 0, mi = 0x3f3f3f3f; for (auto u : dec) { if ((lst + u.fr + 2) >= d) { ans += u.se; mi = min(mi, u.fr); lst = u.fr; } else { ans += (u.se - 1); } } mi++; if (mi == d) { lst = 0; ans++; } return {mi, ans}; } int32_t main() { cin >> n >> d; for (int i = 1 ; i < n; i++) { int x; cin >> x; adj[x].push_back(i); } cout << calc(0).se << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...