Cod sursa(job #2028652)

Utilizator eduardandrei20Nechifor Eduard Andrei eduardandrei20 Data 28 septembrie 2017 11:34:18
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int rang[100000], TT[100000];
int root(int nod){

int R=nod,next;
while(TT[R]!=R)R=TT[R];

while(TT[nod]!=nod){

    next=TT[nod];
    TT[nod]=R;
    nod=next;


}
return R;

}

void unite(int x,int y)
{
  if(rang[x]<rang[y])TT[y]=x;
  else TT[x]=y;
  if(rang[x]==rang[y])rang[y]++;

}


int main()
{
int N,M,op,x,y;
in>>N>>M;
int i;
for (i=1 ;i<=N ;++i){
    TT[i]=i;
    rang[i]=1;

}
for(i=1 ; i<=M ; ++i)
{
    in>>op>>x>>y;

if(op==1){

    int rootX,rootY;
    rootX=root(x);
    rootY=root(y);
    if(rootX!=rootY)unite(x,y);



}
else {


    if(root(x)!=root(y))out<<"NU\n";
    else out<<"DA\n";
}




}




    return 0;
}