제출 #1035865

#제출 시각아이디문제언어결과실행 시간메모리
1035865phoenixCat in a tree (BOI17_catinatree)C++17
51 / 100
1066 ms16020 KiB
#include <bits/stdc++.h> using namespace std; const int N = 200200; int n, D; int p[N]; int d[N]; vector<int> g[N]; int iter; int us[N]; int dst[N]; queue<int> q; void del(int v) { q.push(v); us[v] = iter; dst[v] = 0; while (!q.empty()) { v = q.front(); q.pop(); if (dst[v] == D - 1) continue; for (int to : g[v]) { if (us[to] != iter) { us[to] = iter; dst[to] = dst[v] + 1; q.push(to); } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> D; for (int i = 1; i < n; i++) { cin >> p[i]; g[p[i]].push_back(i); g[i].push_back(p[i]); } for (int i = 1; i < n; i++) { d[i] = d[p[i]] + 1; } vector<int> ord(n); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](int a, int b) { return d[a] > d[b]; }); int res = 0; for (int c : ord) { if (us[c]) continue; iter++; res++; del(c); } cout << res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...