# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1110987 | anmattroi | Cat in a tree (BOI17_catinatree) | C++14 | 58 ms | 25968 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.
#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
int n, d;
vector<int> adj[maxn];
int f[maxn];
int ds = 0;
void dfs(int u, int dad) {
f[u] = INT_MAX/3;
vector<int> nho;
for (int v : adj[u]) {
if (v == dad) continue;
dfs(v, u);
nho.emplace_back(f[v]+1);
}
sort(nho.begin(), nho.end());
reverse(nho.begin(), nho.end());
while (nho.size() >= 2 && nho[int(nho.size())-1] + nho[int(nho.size())-2] < d) {--ds; nho.pop_back();}
if (!nho.empty()) f[u] = nho.back();
if (f[u] >= d) {
++ds;
f[u] = 0;
}
}
int main() {
if (fopen("FARcatNOUGH.INP", "r")) {
freopen("FARENOUGH.INP", "r", stdin);
freopen("FARENOUGH.OUT", "w", stdout);
}
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> d;
for (int u = 2; u <= n; u++) {
int v; cin >> v;
++v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
dfs(1, 0);
cout << ds;
}
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... |