이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const ll inf = 1e18;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 2e5;
int n, d;
vector<int> g[mxn];
int depth[mxn];
void dfs(int u, int p){
for(auto e : g[u]){
if(e == p) continue;
depth[e] = depth[u] + 1;
dfs(e, u);
}
}
bool deleted[mxn];
void _remove(int u, int p, int dis){
deleted[u] = true;
if(dis == d) return;
for(auto e : g[u]){
if(e == p) continue;
_remove(e, u, dis + 1);
}
}
void solve(){
cin >> n >> d;
--d;
fr(i, 0, n-1){
int x;
cin >> x;
g[x].pb(i+1);
g[i+1].pb(x);
}
dfs(0, 0);
vector<int> v;
fr(i, 0, n) v.pb(i);
sort(all(v), [](const int &i, const int &j){
return depth[i] > depth[j];
});
int ans = 0;
for(auto u : v){
if(deleted[u]) continue;
_remove(u, u, 0);
++ans;
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |