제출 #1058775

#제출 시각아이디문제언어결과실행 시간메모리
1058775gygCat in a tree (BOI17_catinatree)C++17
11 / 100
14 ms9308 KiB
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int MAX_N = 1500 + 5, INF = 1e9; int n, k; 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); } } int query(int u) { int score = 0; for (int v = 1; v <= n; v++) score += (dist[u][v] < k); return score; } vector<pii> order; array<bool, MAX_N> used; int comp() { for (int u = 1; u <= n; u++) order.push_back({query(u), u}); sort(order.begin(), order.end()); int ans = 0; for (pii x : order) { int u = x.second; if (used[u]) continue; ans++; for (int v = 1; v <= n; v++) if (dist[u][v] < k) used[v] = true; } return ans; } int main() { // freopen("cat.in", "r", stdin); cin >> n >> k; 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'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...