제출 #1035840

#제출 시각아이디문제언어결과실행 시간메모리
1035840phoenixCat in a tree (BOI17_catinatree)C++17
51 / 100
1073 ms16184 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]; bool us[N]; int dst[N]; queue<int> q; void del(int v) { vector<int> vis(n); q.push(v); vis[v] = true; dst[v] = 0; while (!q.empty()) { v = q.front(); us[v] = true; q.pop(); if (dst[v] == D - 1) continue; for (int to : g[v]) { if (!vis[to]) { vis[to] = true; 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; res++; del(c); } cout << res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...