Cod sursa(job #1386623)

Utilizator pepsiM4A1Ozturk Arif pepsiM4A1 Data 13 martie 2015 09:34:31
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <stdio.h>
int t[100001];
int n,m;
int main()
{
    freopen ("disjoint.in","r",stdin);
    freopen ("disjoint.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
            int tip,p1,p2;
            scanf("%d%d%d",&tip,&p1,&p2);
            if(tip==1)
            {
                      int h1=0,alt=p1;
                      while(t[alt]!=0)
                      {
                                      h1++;
                                      alt=t[alt];
                      }
                      int alt2=p2;
                      while(t[alt2]!=0)
                      {
                                       h1--;
                                       alt2=t[alt2];
                      }
                      if(h1>=0) t[alt2]=alt;
                      else t[alt]=alt2;
            }
            else
            {
                while(t[p1]!=0)
                {
                               p1=t[p1];
                }
                while(t[p2]!=0)
                {
                               p2=t[p2];
                }
                if(p1==p2) printf("DA\n");
                else printf("NU\n");      
            }
    }
}