제출 #378168

#제출 시각아이디문제언어결과실행 시간메모리
378168YJUCat in a tree (BOI17_catinatree)C++14
100 / 100
139 ms24616 KiB
#include<bits/stdc++.h> #pragma GCC optimize("unroll-loops,no-stack-protector,Ofast") using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll,ll> pll; const ll N=2e5+5; const ll MOD=1e9+7; const ld pi=acos(-1); #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<=n;i++) #define pb push_back #define mp make_pair #define X first #define Y second #define lwb lower_bound #define SZ(_a) (ll)_a.size() vector<ll> v[N]; ll n,x,d; pll DFS(ll nd,ll pa){ vector<pll> lis; for(auto i:v[nd]){ if(i==pa)continue; lis.pb(DFS(i,nd)); } if(SZ(lis)==0){return mp(1,1);} sort(lis.begin(),lis.end(),greater<pll>()); ll pos=0; for(int i=1;i<SZ(lis);i++){ if(lis[i-1].X+lis[i].X>=d)pos=i; } ll ret=0; REP(i,SZ(lis))ret+=lis[i].Y-1; ret+=pos+1; if(lis[pos].X>=d)return mp(1,ret+1); else return mp(lis[pos].X+1,ret); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>n>>d; for(int i=1;i<n;i++)cin>>x,v[x].pb(i),v[i].pb(x); cout<<DFS(0,-1).Y<<"\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...