# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1115821 | vjudge1 | Cat in a tree (BOI17_catinatree) | C++17 | 94 ms | 22188 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> g[200005];
int d;
pair<int, int> f[200005];
void dfs(int x) {
if (g[x].size() == 0) {
f[x] = {1, 0};
return;
}
int c = 0;
vector<int> v;
for (int w : g[x]) {
dfs(w);
c += f[w].first;
v.push_back(f[w].second + 1);
}
sort(v.begin(), v.end());
int j = -1;
for (int i = 0; i < v.size() - 1; i++) {
if (v[i] + v[i + 1] < d) {
c--;
}
else {
j = i;
break;
}
}
if (j == -1) j = v.size() - 1;
if (v[j] >= d) {
v[j] = 0;
c++;
}
f[x] = {c, v[j]};
/*cout << x << " " << f[x].first << " " << j << " " << v[j] << '\n';
for (int w : v) cout << w << " ";
cout << '\n';*/
}
int main() {
int n;
cin >> n >> d;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
g[x].push_back(i);
}
dfs(0);
cout << f[0].first;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |