#include <cmath>
#include <iostream>
#include <set>
#include <climits>
#include <algorithm>
#include <cassert>
#include <vector>
#include <iomanip>
#include <type_traits>
#include <string>
#include <queue>
#include <map>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
vector<vector<int>> adj;
vector<pair<int,int>> vec;
vector<int> parent;
vector<bool> isValid;
bool comp (pair<int,int> p1, pair<int,int> p2) {
return (p1.second > p2.second);
}
void find_parent (int curNode, int prevNode) {
parent[curNode] = prevNode;
for (int i: adj[curNode]) if (i != prevNode) find_parent(i, curNode);
}
void go_down (int curNode, int d) {
if (d == 0) return;
if (!isValid[curNode]) return;
isValid[curNode] = false;
for (int i: adj[curNode]) go_down(i, d - 1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, D;
cin >> N >> D;
adj.resize(N), vec.assign(N, {-1, -1}), isValid.assign(N, true), parent.resize(N);
for (int i = 1; i < N; i++) {
int x; cin >> x;
adj[x].push_back(i), adj[i].push_back(x);
}
queue<pair<int,int>> q;
q.push({0, 0});
while (!q.empty()) {
pair<int,int> pr = q.front();
q.pop();
if (vec[pr.first].first != -1) {
continue;
}
vec[pr.first] = pr;
for (int i: adj[pr.first]) {
q.push({i, pr.second + 1});
}
}
find_parent(0, -1);
sort(vec.begin(), vec.end(), comp);
int ans = 0;
for (auto& p: vec) {
if (!isValid[p.first]) {
continue;
}
ans++;
go_down(p.first, D);
}
cout << ans;
}
Compilation message
catinatree.cpp:15: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
15 | #pragma GCC optimization ("O3")
|
catinatree.cpp:16: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
16 | #pragma GCC optimization ("unroll-loops")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |