제출 #1103136

#제출 시각아이디문제언어결과실행 시간메모리
1103136alexddCat in a tree (BOI17_catinatree)C++17
100 / 100
47 ms26188 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int INF = 1e9; int n,d,root; vector<int> con[200005]; int parent[200005]; int cnt[200005],injos[200005]; void dfs(int nod) { cnt[nod]=1; injos[nod]=0; for(int adj:con[nod]) { if(adj==parent[nod]) continue; parent[adj]=nod; dfs(adj); cnt[nod] += cnt[adj]; if(injos[nod]+injos[adj]+1 < d) { cnt[nod]--; injos[nod] = max(injos[nod], injos[adj]+1); } else { injos[nod] = min(injos[nod], injos[adj]+1); } } } signed main() { ios_base::sync_with_stdio(0);cin.tie(0); cin>>n>>d; int u; for(int i=2;i<=n;i++) { cin>>u; u++; con[i].push_back(u); con[u].push_back(i); } root=1; for(int i=1;i<=n;i++) if((int)con[i].size()>1) root=i; //cerr<<root<<" root\n"; dfs(root); cout<<cnt[root]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...