이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
using namespace std;
#define maxn 200005
ll n,d;
vector<ll> g[maxn];
pll dfs(ll u,ll p){
vector<ll> v; v.pb(0);
ll cnt = 1;
for(ll s : g[u]){
if(s==p) continue;
pll cur = dfs(s,u);
v.pb(cur.sc);
cnt+=cur.fi;
}
sort(all(v));
reverse(all(v));
while(v.size()>1&&v[v.size()-1] + v[v.size()-2]<d){
v.popb(); cnt--;
}
return {cnt,v.back()+1};
}
void tc(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> d;
for(ll i = 2;i<=n;i++){
ll x; cin >> x;
x++;
g[x].pb(i);
g[i].pb(x);
}
pll ans = dfs(1,1);
cout<<ans.fi<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
int t; t = 1;
while(t--){
tc();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |