Cod sursa(job #1795675)

Utilizator nnnmmmcioltan alex nnnmmm Data 2 noiembrie 2016 19:44:51
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda cerculdeinfo-lectia5-paduri.heap.aib Marime 0.82 kb
#include<cstdio>
int sef[100001];
int gaseste(int x)
{
 int poz=x;
 while(sef[poz])
       {
        poz=sef[poz];
       }
 //Compresia caii
 int poz1=x;
 while(sef[poz1])
       {
        int c=poz1;
        poz1=sef[poz1];
        sef[c]=poz;
       }
 return poz;
}
void uneste(int x,int y)
{
 sef[gaseste(x)]=gaseste(y);
}
int main()
{
 freopen("disjoint.in","r",stdin);
 freopen("disjoint.out","w",stdout);
 int nr_elem,nr_op;
 scanf("%d %d ",&nr_elem,&nr_op);
 for(int i=1;i<=nr_op;i++)
     {
      int tip,x,y;
      scanf("%d %d %d ",&tip,&x,&y);
      if(tip==1)
         {
          uneste(x,y);
         }
      else
         if(gaseste(x)==gaseste(y))
            printf("DA\n");
         else
            printf("NU\n");
     }
 fclose(stdin);
 fclose(stdout);
return 0;
}