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

Tác giả: flashmt

Ngôn ngữ: Pascal

const fi='';
      fo='';
      maxn=10001;
var n,k,re:longint;
    b,sl,pos,cur:array[1..maxn] of longint;
    a:array[1..maxn shl 1] of longint;

procedure rf;
var i:longint;
begin
     assign(input,fi); reset(input);
     read(k,n);
     for i:=1 to n-1 do
     begin
          read(b[i]);
          inc(sl[b[i]]);
     end;
     pos[1]:=1; cur[1]:=1;
     for i:=2 to n+1 do
     begin
          pos[i]:=pos[i-1]+sl[i-1];
          cur[i]:=pos[i];
     end;
     for i:=1 to n-1 do
     begin
          a[cur[b[i]]]:=i+1;
          inc(cur[b[i]]);
     end;
     close(input);
end;

function dfs(x:longint):longint;
var i,r:longint;
begin
     r:=1;
     for i:=pos[x] to pos[x+1]-1 do
         r:=r+dfs(a[i]);
     if r>=k then
     begin
          r:=0;
          inc(re);
     end;
     dfs:=r;
end;

procedure pr;
var i:longint;
begin
     re:=0;
     i:=dfs(1);
end;

procedure wf;
begin
     assign(output,fo); rewrite(output);
     writeln(re);
     close(output);
end;

begin
     rf;
     pr;
     wf;
end.

Download