Cod sursa(job #2058730)

Utilizator AnduRazvanMindrescu Andu AnduRazvan Data 6 noiembrie 2017 01:25:44
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <cstdio>

using namespace std;
int n,m,op,x,y,c1,c2;
int t[100001],h[100001];
int Find(int xx)
{ while(xx!=t[xx]) xx=t[xx];
    return xx;
}
void Union(int c,int cc)
{ if(h[c]>h[cc]) t[cc]=c;
  else if(h[c]<h[cc]) t[c]=cc;
  else if(h[c]==h[cc]) {t[cc]=c;h[cc]++;}
}
int main()
{ FILE *f,*g;
 f=fopen("disjoint.in","r");
 g=fopen("disjoint.out","w");
    fscanf(f,"%d %d",&n,&m);
  int i;
   for(i=1;i<=n;i++)
    {t[i]=i;h[i]=1;}
    for(i=1;i<=m;i++)
    { fscanf(f,"%d %d %d",&op,&x,&y);
        if(op==1)
           { c1=Find(x);
             c2=Find(y);
             Union (c1,c2);
           }
         else
          { c1=Find(x);
            c2=Find(y);
            if(c1==c2) fprintf(g,"DA\n");
            else fprintf(g,"NU\n");
          }
    }
    return 0;
}