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<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
constexpr int maxn = 2e5;
basic_string <int> children[maxn];
int d;
pii dfs(int p)
{
vector <pii> childs;
if (children[p].size() == 0)
{
return pii(1, 1); // Dist, num
}
for (int i : children[p])
{
childs.emplace_back(dfs(i));
}
childs.emplace_back(0, 1);
sort(childs.begin(), childs.end());
pii out = pii(d + 1, 0);
pii pot;
while (!childs.empty())
{
pot = childs.back();
childs.pop_back();
if (pot.first + out.first >= d)
{
out.second += pot.second;
out.first = pot.first;
} else break;
}
out.second += pot.second - 1;
while (!childs.empty())
{
pot = childs.back();
childs.pop_back();
out.second += pot.second - 1;
}
out.first ++;
return out;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n >> d;
int a;
for (int i = 1; i < n; i++)
{
cin >> a;
children[a].push_back(i);
}
int root = 0;
pii output = dfs(root);
cout << output.second << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |