V8ORG - Tổ chức đối lập

Tác giả: RR

Ngôn ngữ: Pascal

PROGRAM V8ORG;
CONST
  fi='';
  fo='';
  maxn=10000;
VAR
  n,k:integer;
  sc:array[1..maxn] of integer;
  son:array[1..maxn,1..maxn] of integer;
  sl:array[1..maxn] of integer;
  dd:integer;
Procedure ReadInput;
Var
  f:text;
  u,i:integer;
Begin
  Assign(f,fi); Reset(f);
  Readln(f,k);
  Readln(f,n);
  For i:=2 to n do
    begin
      Read(f,u);
      inc(sc[u]);
      son[u,sc[u]]:=i;
    end;
End;
Procedure DFS(u:integer);
Var
  i,v:integer;
Begin
  sl[u]:=1;
  For i:=1 to sc[u] do
    begin
      v:=son[u,i];
      DFS(v);
      sl[u]:=sl[u]+sl[v];
    end;
  If sl[u]>=k then
  begin
    inc(dd);
    sl[u]:=0;
  end;
End;
Procedure WriteOutput;
Var
  f:text;
Begin
  Assign(f,fo); Rewrite(f);
  Writeln(f,dd);
  Close(f);
End;
BEGIN
  ReadInput;
  DFS(1);
  WriteOutput;
END.

Download