Submission #1299805

#TimeUsernameProblemLanguageResultExecution timeMemory
1299805aicloCat in a tree (BOI17_catinatree)C++20
0 / 100
1 ms572 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, D; cin >> N >> D; vector<vector<int>> g(N); for (int i = 1; i < N; i++) { int p; cin >> p; g[p].push_back(i); g[i].push_back(p); } if (D <= 1) { // Bez ograniczenia (odległość >= 0 lub >= 1 zawsze spełniona) cout << N << "\n"; return 0; } // 1. Obliczamy głębokości (BFS od korzenia 0) vector<int> depth(N, -1); queue<int> q; depth[0] = 0; q.push(0); while (!q.empty()) { int v = q.front(); q.pop(); for (int u : g[v]) { if (depth[u] == -1) { depth[u] = depth[v] + 1; q.push(u); } } } // 2. Kolejka maksymalna według głębokości priority_queue<pair<int,int>> pq; for (int i = 0; i < N; i++) { pq.push({depth[i], i}); // najgłębszy na górze } vector<bool> alive(N, true); int marked = 0; // 3. Greedy: bierzemy najgłębszy żywy wierzchołek while (!pq.empty()) { auto [d, v] = pq.top(); pq.pop(); if (!alive[v]) continue; marked++; // 4. Usuwamy w promieniu D-1 (BFS ograniczony) queue<pair<int,int>> qq; qq.push({v, 0}); alive[v] = false; while (!qq.empty()) { auto [u, dist] = qq.front(); qq.pop(); if (dist == D - 1) continue; for (int w : g[u]) { if (alive[w]) { alive[w] = false; qq.push({w, dist + 1}); } } } } cout << marked << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...