Cod sursa(job #401967)

Utilizator nickyyLal Daniel Emanuel nickyy Data 23 februarie 2010 11:26:53
Problema Paduri de multimi disjuncte Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.96 kb
const infile='disjoint.in';
  outfile='disjoint.out';
  maxn=100002;
var h,tata:array[0..maxn] of longint;
  n,m:longint;

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

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

 procedure solve;
 var op:byte;
   x,y,k,i,j:longint;
 begin
   assign(input,infile); reset(input);
   assign(output,outfile); rewrite(output);
   readln(n,m);
   for i:=1 to n do begin tata[i]:=i; h[i]:=1; end;
   for k:=1 to m do begin
     readln(op,x,y);
     i:=multime(x); j:=multime(y);
     if op=1 then
       if i<>j then reuniune(i,j)
       else
     else if i<>j then writeln('NU')
       else writeln('DA');
     end;
   close(input); close(output);
 end;

begin
solve;
end.