제출 #528830

#제출 시각아이디문제언어결과실행 시간메모리
528830Alex_tz307Cat in a tree (BOI17_catinatree)C++17
100 / 100
165 ms15452 KiB
#include <bits/stdc++.h> using namespace std; const int kN = 2e5; vector<int> g[kN]; int mark[kN]; void dfs(int u, int d) { mark[u] = d; for (int v : g[u]) { if (mark[v] < d - 1) { dfs(v, d - 1); } } } void testCase() { int n, d; cin >> n >> d; vector<int> dep(n); vector<pair<int, int>> nodes(n, {0, 0}); for (int v = 1; v < n; ++v) { int u; cin >> u; g[u].emplace_back(v); g[v].emplace_back(u); dep[v] = dep[u] + 1; nodes[v] = {dep[v], v}; } sort(nodes.begin(), nodes.end(), greater<pair<int, int>>()); int ans = 0; for (auto it : nodes) { if (mark[it.second] == 0) { ans += 1; dfs(it.second, d); } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int tests = 1; for (int tc = 0; tc < tests; ++tc) { testCase(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...