제출 #1106123

#제출 시각아이디문제언어결과실행 시간메모리
1106123ortsacCat in a tree (BOI17_catinatree)C++17
100 / 100
61 ms17096 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; for (auto u : dec) { if ((lst + u.fr + 2) >= d) { ans += u.se; lst = u.fr; } else { ans += (u.se - 1); } } lst++; if (lst == d) { lst = 0; ans++; } return {lst, 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...