# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1254923 | mingga | Cat in a tree (BOI17_catinatree) | C++20 | 31 ms | 15940 KiB |
// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 2e5 + 7;
vector<int> g[N];
int n, f[N], f1[N], d;
void dfs(int u) {
f1[u] = inf;
for(int v : g[u]) {
dfs(v);
if(f1[u] + f1[v] + 1 >= d) {
f[u] += f[v];
f1[u] = min(f1[u], f1[v] + 1);
} else {
f[u] += f[v] - 1;
f1[u] = max(f1[u], f1[v] + 1);
}
}
if(f1[u] >= d) f[u]++, f1[u] = 0;
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n >> d;
for(int i = 1; i < n; i++) {
int p; cin >> p;
g[p].pb(i);
}
dfs(0);
cout << f[0];
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
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... |