제출 #714911

#제출 시각아이디문제언어결과실행 시간메모리
714911MilosMilutinovicCat in a tree (BOI17_catinatree)C++14
100 / 100
110 ms28080 KiB
#include <bits/stdc++.h> using i64 = long long; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, k; std::cin >> n >> k; std::vector<std::vector<int>> adj(n); for (int i = 1; i < n; i++) { int p; std::cin >> p; adj[p].push_back(i); adj[i].push_back(p); } std::function<std::pair<int, int>(int, int)> Dfs = [&](int v, int pv) { std::vector<int> dist(1, 0); int f = 1; for (int u : adj[v]) { if (u == pv) { continue; } std::pair<int, int> x = Dfs(u, v); f += x.first; dist.push_back(x.second); } std::sort(dist.begin(), dist.end()); std::reverse(dist.begin(), dist.end()); while (dist.size() > 1 && dist.back() + dist[dist.size() - 2] < k) { dist.pop_back(); f -= 1; } return std::make_pair(f, dist.back() + 1); }; std::cout << Dfs(0, 0).first << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...