제출 #689919

#제출 시각아이디문제언어결과실행 시간메모리
689919Darren0724Cat in a tree (BOI17_catinatree)C++17
100 / 100
111 ms37048 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define all(x) x.begin(),x.end() int n,r; vector<vector<int>> adj; vector<multiset<int>> v; vector<int> dp; void dfs(int k,int d){ int big=n; for(int j:adj[k]){ dfs(j,d+1); if(v[big].size()<v[j].size()){ big=j; } dp[k]+=dp[j]; } swap(v[k],v[big]); for(int j:adj[k]){ if(j==big){ continue; } for(int i:v[j]){ v[k].insert(i); } } while(v[k].size()){ int a=*v[k].rbegin(); if(a>=d+r){ dp[k]++; v[k].erase(v[k].find(a)); } else{ break; } } while(v[k].size()>=2){ int a,b; auto it=v[k].begin(); a=*it; it++; b=*it; if(a+b-d*2<r){ v[k].erase(v[k].find(a)); } else{ break; } } if(v[k].size()==0){ v[k].insert(d); } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin>>n>>r; v.resize(n+1); adj.resize(n); dp.resize(n+1); for(int i=1;i<n;i++){ int p;cin>>p; adj[p].push_back(i); } dfs(0,0); cout<<dp[0]+v[0].size()<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...