Cod sursa(job #581170)

Utilizator andreifirstCioara Andrei Ioan andreifirst Data 13 aprilie 2011 20:42:00
Problema Paduri de multimi disjuncte Scor 70
Compilator fpc Status done
Runda Arhiva educationala Marime 1.2 kb
type muchie=^nod;
     nod = record n:longint; a:muchie; end;

var v:array [1..100000] of muchie;
    chk:array[1..100000] of boolean;
    buf1, buf2:array [1..1 shl 17] of char;
    i, m, n, c, x, y:longint;
    p:muchie;
    ok:boolean;
    f, g:text;

procedure dfs (a:muchie; b:longint);
  begin
  chk[b]:=true;
  if b=y then ok:=true;
  while (a <> nil) and (ok=false) do
    begin
    if chk[a^.n] = false then dfs(v[a^.n], a^.n);
    a:=a^.a;
    end;
  end;

begin
assign (f, 'disjoint.in'); settextbuf (f, buf1); reset (f);
assign (g, 'disjoint.out'); settextbuf (g, buf2); rewrite (g);

read (f, n, m);
for i := 1 to m do
  begin
  read (f, c, x, y);
  case c of
    1:begin
      if v[x]= nil then begin new(v[x]); v[x]^.a:=nil; v[x]^.n:=y; end
                   else begin new(p); p^.n:=y; p^.a:=v[x]; v[x]:=p; end;
      if v[y]= nil then begin new(v[y]); v[y]^.a:=nil; v[y]^.n:=x; end
                   else begin new(p); p^.n:=x; p^.a:=v[y]; v[y]:=p; end;
      end;
    2:begin
      ok:= false;
      fillchar (chk, n+1, false);
      dfs(v[x], x);
      if ok then writeln (g, 'DA') else writeln (g, 'NU');
      end;
    end;
  end;

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