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;
typedef long long ll;
typedef pair <int, int> pii;
vector <vector <int>> adj;
vector <pii> dp;
int d;
void dfs(int u) {
if (adj[u].empty()) {
dp[u] = make_pair(1, 0);
return ;
}
int ans = 0;
vector <pii> a;
for(int v : adj[u]) {
dfs(v);
a.push_back(dp[v]);
ans += dp[v].first;
}
sort(a.begin(), a.end(), [] (const pii &a, const pii &b) {
return a.second < b.second;
});
int dist = a[0].second + 1;
int size = adj[u].size();
for(int i = 1; i < size; ++i) {
if (a[i-1].second + 2 + a[i].second < d) {
ans--;
dist = a[i].second + 1;
}
else {
if (a[i-1].second + 1 >= d) {
ans++;
dist = 0;
}
break;
}
}
dp[u] = make_pair(ans, dist);
}
int main(const int argc, const char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n >> d;
adj.resize(n);
dp.resize(n);
for(int i = 1; i < n; ++i) {
int p;
cin >> p;
adj[p].push_back(i);
}
dfs(0);
cout << dp[0].first;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |