fork download
  1. #include <bits/stdc++.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <set>
  5. using namespace std;
  6. #define el endl
  7. #define dl double
  8. #define ll long long
  9. #define faster ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  10. #define op 1005
  11. #define _c const int
  12. _c N = op;
  13. vector <int>adj[N];
  14. int a[N][N];
  15. int k,n;
  16. int cnt = 0;
  17. int dfs(int u){
  18. int tmp_dinh = 1;
  19. for(int v:adj[u]){
  20. tmp_dinh+=dfs(v);
  21. }
  22. if(tmp_dinh >= k){
  23. cnt++;
  24. return 0;
  25. }
  26. return tmp_dinh;
  27. }
  28. void inp(){
  29. cin >> k >> n;
  30. for(int i = 2; i <= n; i++){
  31. int x; cin >> x;
  32. adj[x].push_back(i);
  33. }
  34. dfs(1);
  35. cout << cnt;
  36. }
  37. int main(){
  38. inp();
  39. faster;
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5288KB
stdin
3
14
1 1 2 2 3 2 3 6 6 6 7 4 7
3
14
1 1 2 2 3 2 3 6 6 6 7 4 7
stdout
4