Submission #1106120

#TimeUsernameProblemLanguageResultExecution timeMemory
1106120ortsacCat in a tree (BOI17_catinatree)C++17
0 / 100
3 ms5140 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; int sum = 0; 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(lst, 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"; }

Compilation message (stderr)

catinatree.cpp: In function 'std::pair<int, int> calc(int)':
catinatree.cpp:18:9: warning: unused variable 'sum' [-Wunused-variable]
   18 |     int sum = 0;
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...