제출 #412951

#제출 시각아이디문제언어결과실행 시간메모리
412951MohamedAhmed04Cat in a tree (BOI17_catinatree)C++14
100 / 100
96 ms24104 KiB
#include <bits/stdc++.h> using namespace std ; const int MAX = 2e5 + 10 ; int dep[MAX] ; int n , d ; vector< vector<int> >adj(MAX) ; int ans = 0 ; int dfs(int node) { vector<int>v ; ans++ ; v.push_back(dep[node]) ; for(auto &child : adj[node]) { dep[child] = dep[node] + 1 ; v.push_back(dfs(child)) ; } sort(v.begin() , v.end()) ; reverse(v.begin() , v.end()) ; while(v.size() >= 2 && v[v.size()-1] + v[v.size()-2] - 2*dep[node] < d) v.pop_back() , ans-- ; return v.back() ; } int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n>>d ; for(int i = 2 ; i <= n ; ++i) { int p ; cin>>p ; adj[p+1].push_back(i) ; } dfs(1) ; return cout<<ans<<"\n" , 0 ; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...