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

Tác giả: ladpro98

Ngôn ngữ: Pascal

program v8org;
uses    math;
const   maxn=12345;
        fi='';
var     adj,link:array[1..maxn] of longint;
        head,f:array[1..maxn] of longint;
        k,n,m,res:longint;

procedure input;
var     inp:text;
        i,p:longint;
begin
        assign(inp,fi);reset(inp);
        readln(inp,k);readln(inp,n);
        for i:=2 to n do
        begin
                read(inp,p);
                inc(m);
                adj[m]:=i;
                link[m]:=head[p];
                head[p]:=m;
        end;
        close(inp);
end;

procedure dfs(i:longint);
var     j:longint;
begin
        if head[i]=0 then
        begin
                if k>1 then
                f[i]:=1
                else
                begin
                        f[i]:=0;
                        inc(res);
                end;
                exit;
        end;
        j:=head[i];
        f[i]:=1;
        while j<>0 do
        begin
                dfs(adj[j]);
                inc(f[i],f[adj[j]]);
                j:=link[j];
        end;
        if f[i]>=k then
        begin
                f[i]:=0;
                inc(res);
        end;
end;

begin
        input;
        dfs(1);
        write(res);
end.

Download