fork download
  1. {$M 400000000}
  2. const maxn = 10000;
  3. fi = '';
  4. fo = '';
  5. var k,n,res:word;
  6. a: array[1..maxn] of word;
  7. dp: array[1..maxn] of word;
  8. g: array[1..maxn,0..maxn] of word;
  9. procedure enter;
  10. var i,u:word;
  11. begin
  12. assign(input,fi);
  13. reset(input);
  14. readln(k);
  15. readln(n);
  16. for i:=1 to n do
  17. begin
  18. dp[i]:=0;
  19. g[i,0]:=0;
  20. end;
  21. dp[1]:=1;
  22. a[1]:=0;
  23. for i:=2 to n do
  24. begin
  25. read(a[i]);
  26. inc(g[a[i],0]);
  27. g[a[i],g[a[i],0]]:=i;
  28. u:=i;
  29. repeat
  30. inc(dp[u]);
  31. u:=a[u];
  32. until u=0;
  33. end;
  34. close(input);
  35. end;
  36. procedure dfs(i:longint);
  37. var u,j:word;
  38. begin
  39. for j:=1 to g[i,0] do
  40. dfs(g[i,j]);
  41. if dp[i]>=k then
  42. begin
  43. inc(res);
  44. u:=a[i];
  45. repeat
  46. dec(dp[u],dp[i]);
  47. u:=a[u]
  48. until u=0;
  49. dp[i]:=0;
  50. end;
  51. end;
  52. procedure solve;
  53. begin
  54. assign(output,fo);
  55. rewrite(output);
  56. res:=0;
  57. dfs(1);
  58. writeln(res);
  59. close(output);
  60. end;
  61. begin
  62. enter;
  63. solve;
  64. end.
Success #stdin #stdout 0s 195648KB
stdin
3
14
1 1 2 2 3 2 3 6 6 6 7 4 7
stdout
4