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;
const int MAX_N = 1500 + 5, INF = 1e9;
int n, d;
array<vector<int>, MAX_N> adj;
array<array<int, MAX_N>, MAX_N> dist;
array<bool, MAX_N> seen;
void dfs(int u, int orig, int d = 0) {
seen[u] = true, dist[orig][u] = d;
for (int v : adj[u])
if (!seen[v]) dfs(v, orig, d + 1);
}
void precomp() {
for (int u = 1; u <= n; u++) {
fill(seen.begin(), seen.end(), false);
dfs(u, u);
}
}
array<unordered_set<int>, MAX_N> range;
array<bool, MAX_N> del;
int comp() {
for (int u = 1; u <= n; u++)
for (int v = 1; v <= n; v++)
if (dist[u][v] < d) range[u].insert(v);
int ans = 0;
while (true) {
int min_siz = INF, best = -1;
for (int u = 1; u <= n; u++) {
if (del[u]) continue;
if (range[u].size() >= min_siz) continue;
min_siz = range[u].size(), best = u;
}
if (best == -1) break;
ans++;
for (int u : range[best]) del[u] = true;
for (int u = 1; u <= n; u++) {
if (del[u]) continue;
vector<int> to_del;
for (int v : range[u])
if (del[v]) to_del.push_back(v);
for (int v : to_del) range[u].erase(v);
}
}
return ans;
}
int main() {
// freopen("cat.in", "r", stdin);
cin >> n >> d; assert(n <= 1500);
for (int u = 2; u <= n; u++) {
int v; cin >> v; v++;
adj[u].push_back(v), adj[v].push_back(u);
}
precomp();
int ans = comp();
cout << ans << '\n';
}
Compilation message (stderr)
catinatree.cpp: In function 'int comp()':
catinatree.cpp:34:33: warning: comparison of integer expressions of different signedness: 'std::unordered_set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
34 | if (range[u].size() >= min_siz) continue;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |