Cod sursa(job #2058726)

Utilizator AnduRazvanMindrescu Andu AnduRazvan Data 6 noiembrie 2017 01:14:13
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <cstdio>

using namespace std;
int n,m,op,x,y,c1,c2;
int t[100001];
int Find(int xx)
{ while(xx!=t[xx]) xx=t[xx];
    return xx;
}

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;
    for(i=1;i<=m;i++)
    { fscanf(f,"%d %d %d",&op,&x,&y);
        if(op==1)
           { c1=Find(x);
             c2=Find(y);
             t[c1]=c2;
           }
         else
          { c1=Find(x);
            c2=Find(y);
            if(c1==c2) fprintf(g,"DA\n");
            else fprintf(g,"NU\n");
          }
    }
    return 0;
}