Submission #1058647

#TimeUsernameProblemLanguageResultExecution timeMemory
1058647gygCat in a tree (BOI17_catinatree)C++17
11 / 100
1 ms604 KiB
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int MAX_N = 18 + 5, INF = 1e9; int n, d; array<vector<int>, MAX_N> adj; array<array<int, MAX_N>, MAX_N> dist; void precomp() { for (int u = 1; u <= n; u++) { fill(dist[u].begin(), dist[u].end(), INF); for (int v : adj[u]) dist[u][v] = 1; dist[u][u] = 0; } for (int u = 1; u <= n; u++) for (int v = 1; v <= n; v++) for (int w = 1; w <= n; w++) dist[u][v] = min(dist[u][v], dist[u][w] + dist[w][v]); } vector<pii> order; array<bool, MAX_N> del; int comp() { for (int u = 1; u <= n; u++) { int score = 0; for (int v = 1; v <= n; v++) score += (dist[u][v] < d); order.push_back({score, u}); } sort(order.begin(), order.end()); int ans = 0; for (pii x : order) { int u = x.second; if (del[u]) continue; ans++; for (int v = 1; v <= n; v++) if (dist[u][v] < d) del[v] = true; } return ans; } int main() { // freopen("cat.in", "r", stdin); cin >> n >> d; assert(n <= 18); 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'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...