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<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 1e6 + 10;
int D[MAXN], dp[MAXN], n, d;
vector<int> adj[MAXN];
// check n == 1
void dfs(int v, int p) {
if (adj[v].size() == 1 && p) {
D[v] = 0;
dp[v] = 1;
return;
}
D[v] = MAXN;
int mn = MAXN, mx = -1;
for (int u : adj[v]) {
if (u == p) continue;
dfs(u, v);
int td = D[u] + 1;
if (2 * td >= d) {
mn = min(mn, td);
dp[v] += dp[u];
D[v] = min(D[v], td);
} else {
dp[v] += dp[u] - 1;
mx = max(mx, td);
}
}
if (mx > -1 && mx + mn >= d) {
dp[v]++;
D[v] = min(D[v], mx);
}
if (D[v] >= d) dp[v]++, D[v] = 0;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> d;
if (n == 1) return cout << 1 << endl, 0;
for (int i = 1; i < n; i++) {
int v;
cin >> v;
adj[i + 1].push_back(v + 1);
adj[v + 1].push_back(i + 1);
}
dfs(1, 0);
cout << dp[1] << endl;
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... |