Submission #887854

#TimeUsernameProblemLanguageResultExecution timeMemory
887854amirhoseinfar1385Cat in a tree (BOI17_catinatree)C++17
100 / 100
95 ms21704 KiB
#include<bits/stdc++.h> using namespace std; int n,d; const int maxn=200000+10; int dp[maxn]; vector<int>adj[maxn]; vector<pair<int,int>>alln; void pre(int u=0,int len=0,int par=-1){ alln.push_back(make_pair(len,u)); for(auto x:adj[u]){ if(x!=par){ pre(x,len+1,u); } } } int res=0; void upd(int u,int len,int par=-1){ if(len<0||len<=dp[u]){ return ; } dp[u]=len; for(auto x:adj[u]){ if(x!=par){ upd(x,len-1,u); } } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>d; d--; for(int i=1;i<=n-1;i++){ int f; cin>>f; adj[f].push_back(i); adj[i].push_back(f); } for(int i=0;i<n;i++){ dp[i]=-1; } pre(); sort(alln.rbegin(),alln.rend()); for(int i=0;i<n;i++){ //cout<<alln[i].first<<" "<<alln[i].second<<" "<<dp[alln[i].second]<<"\n"; if(dp[alln[i].second]==-1){ res++; upd(alln[i].second,d); } } cout<<res<<"\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...