This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5;
int n, d;
int cats[MAX_N], dist[MAX_N];
vector<int> adj[MAX_N];
struct cd {
int cats, dist;
};
void dfs( int u ) {
vector<cd> o;
cats[u] = 0;
dist[u] = n;
o.push_back( { 1, 0 } );
for ( int v: adj[u] ) {
dfs( v );
o.push_back( { cats[v], dist[v] + 1 } );
}
sort( o.begin(), o.end(), []( cd a, cd b ) {
return a.dist < b.dist;
} );
int i;
for ( i = 0; i < o.size() - 1; i++ ) {
if ( o[i].dist + o[i + 1].dist < d )
cats[u] += o[i].cats - 1;
else
break;
}
for ( ; i < o.size(); i++ ) {
cats[u] += o[i].cats;
dist[u] = min( dist[u], o[i].dist );
}
}
int main() {
cin >> n >> d;
for ( int v = 1; v < n; v++ ) {
int p;
cin >> p;
adj[p].push_back( v );
}
dfs( 0 );
cout << cats[0];
return 0;
}
Compilation message (stderr)
catinatree.cpp: In function 'void dfs(int)':
catinatree.cpp:30:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<cd>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for ( i = 0; i < o.size() - 1; i++ ) {
| ~~^~~~~~~~~~~~~~
catinatree.cpp:36:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<cd>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for ( ; i < o.size(); i++ ) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |