# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
347200 | parsabahrami | Cat in a tree (BOI17_catinatree) | C++17 | 110 ms | 14572 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.
// Call my Name and Save me from The Dark
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 2e5 + 10, MOD = 1e9 + 7;
int mx[N], dp[N], n, D;
vector<int> adj[N];
void DFS(int v) {
if (!SZ(adj[v])) {
dp[v] = 1, mx[v] = 0;
return;
}
for (int u : adj[v]) DFS(u);
sort(adj[v].begin(), adj[v].end(), [&](int u, int w) {
return mx[u] > mx[w];
});
int M = MOD, res = 0;
for (int u : adj[v]) {
if (mx[u] + M + 2 >= D) {
res += dp[u], M = mx[u];
} else res += dp[u] - 1;
}
if (M >= D - 1) {
M = -1, res++;
}
dp[v] = res, mx[v] = M + 1;
}
int main() {
scanf("%d%d", &n, &D);
for (int i = 1; i < n; i++) {
int p; scanf("%d", &p);
adj[p].push_back(i);
}
DFS(0);
printf("%d\n", dp[0]);
return 0;
}
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... |