Cod sursa(job #295541)

Utilizator bogdanhm999Casu-Pop Bogdan bogdanhm999 Data 3 aprilie 2009 13:17:37
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <stdio.h>
int t[100005],F;

inline void output(int x){if (x)printf("DA\n");else printf("NU\n");}

void down(int x){
  if (t[x]==x){F=x;return;}
  down(t[x]);
  t[x]=F;
}

void join(int x,int y){
  down(x);down(y);t[t[y]]=t[x];
}

void check(int x,int y){
  down(x);down(y);
  output((t[x]==t[y]));
}
  
int main(){
  freopen("disjoint.in","r",stdin);
  freopen("disjoint.out","w",stdout);
  int N,M,T,x,y,i;

  scanf("%d %d",&N,&M);
  //preprocesare
  for (i=1;i<=N;++i)t[i]=i;
  //
  for (; M; --M){
    scanf("%d %d %d",&T,&x,&y);
    if (T==1)join(x,y);
    else check(x,y);
  }
return 0;
}