Cod sursa(job #401533)

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

 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 gaseste(i:longint):longint;
 var y,t:longint;
 begin
   y:=i; while(tata[y]<>y)do y:=tata[y];
   while(i<>tata[i])do begin t:=tata[i]; tata[i]:=y; i:=t; end;
   gaseste:=y;
 end;

begin
 assign(fi,infile); reset(fi);
 assign(fo,outfile); rewrite(fo);
 readln(fi,n,m);
 for x:=1 to n do begin tata[x]:=x; niv[x]:=1; end;
 while(m>0)do begin
   readln(fi,op,x,y); dec(m);
   i:=gaseste(x); j:=gaseste(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.