Cod sursa(job #401544)

Utilizator nickyyLal Daniel Emanuel nickyy Data 22 februarie 2010 22:10:24
Problema Paduri de multimi disjuncte Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.98 kb
const infile='disjoint.in';
  outfile='disjoint.out';
  maxnm=100001;
var tata,niv:array[0..maxnm]of longint;
  n,m:longint;
  fi,fo:text;

 procedure unire(x,y:longint);
 begin
   if(niv[x]>niv[y])then tata[y]:=x
   else begin
     tata[x]:=y;
     if(niv[x]=niv[y])then inc(niv[y]);
     end;
 end;

 function cauta(x:longint):longint;
 var y,t,r:longint;
 begin
   r:=x; while(tata[r]<>r)do r:=tata[r];
   cauta:=r; y:=x;
   while(y<>tata[y])do begin t:=tata[y]; tata[y]:=r; y:=t; end;
 end;

 procedure solve;
 var op:1..2;
   x,y,i,j:longint;
 begin
   assign(fi,infile); reset(fi);
   assign(fo,outfile); rewrite(fo);
   readln(fi,n,m);
   for i:=1 to n do begin tata[i]:=i; niv[i]:=1; end;
   while(m>0)do begin
     readln(fi,op,x,y); dec(m);
     i:=cauta(x); j:=cauta(y);
     if(op=2)then
       if(i=j)then writeln(fo,'DA')
       else writeln(fo,'Nu')
     else if(i<>j)then unire(i,j);
     end;
   close(fi); close(fo);
 end;

begin
solve;
end.