Cod sursa(job #164438)

Utilizator adalLica Adela adal Data 24 martie 2008 10:58:43
Problema Parcurgere DFS - componente conexe Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 0.92 kb
program df;

type point=^lista;
   lista=record
    inf:longint;
    leg:point;
end;

var l:array[0..1000] of point;
    sel:array[0..1000] of boolean;
    time,nr,m,n,i,j,x,y:longint;
    f,g:text;
    p:point;

procedure df(nod:longint);
var p:point;
begin
    sel[nod]:=true;
    p:=l[nod];
    while p <>nil do begin
        if sel[p^.inf]=false then begin
            df(p^.inf);
        end;
        p:=p^.leg;
    end;
end;


begin
    assign(f,'dfs.in'); reset(f);
    assign(g,'dfs.out'); rewrite(g);
    readln(f,n,m);
    for i:=1 to n do l[i]:=nil;
    for i:=1 to m do begin
        readln(f,x,y);
        new(p); p^.inf:=x; p^.leg:=l[y]; l[y]:=p;
        new(p); p^.inf:=y; p^.leg:=l[x]; l[x]:=p;
    end;
    fillchar(sel, sizeof(sel), 0);
    nr:=0;
    for i:=1 to n do
        if sel[i]=false then begin inc(nr); df(i); end;
    writeln(g,nr);

    close(f); close(g);
end.