이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (200005)
ll N,D;
vector<ll> v[MAXN];
ll depth[MAXN];
bool cannot[MAXN];
void dfs1(ll x,ll p){
for(auto u : v[x]){
if(u != p){
depth[u] = depth[x] + 1;
dfs1(u,x);
}
}
}
void dfs2(ll x,ll distance){
cannot[x] = true;
for(auto u : v[x]){
if(!cannot[u] && distance + 1 <= D){
dfs2(u,distance + 1);
}
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
cin>>N>>D;
for(ll i = 1;i < N;i++){
ll p;
cin>>p; //0-indexed
v[p].push_back(i);
v[i].push_back(p);
}
dfs1(0,-1);
vector<pair<ll,ll> > sortbydepth;
for(ll i = 0;i < N;i++){
sortbydepth.push_back({depth[i],i});
}
sort(sortbydepth.begin(),sortbydepth.end(),greater<pair<ll,ll> >());
ll ans = 0;
for(auto u : sortbydepth){
ll x = u.second;
if(cannot[x]) continue; //node x is within dist D from a node selected so far so we can greedily prove that it is not optimal to delete the selected node (with greater depth) and replace it with node x (with lower depth)
ans++;
dfs2(x,0); //mark the nodes within D distance as cannot be picked
}
cout<<ans<<'\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |