Pagini recente » Cod sursa (job #146781) | Cod sursa (job #637829) | Cod sursa (job #1504073) | Cod sursa (job #417128) | Cod sursa (job #1473128)
#include <iostream>
#include <fstream>
#define NMAX 100020
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N,M;
int rang[NMAX],arbore[NMAX];
void Initiailizare()
{
for(int i=1; i<=N; i++)
{
rang[i]=1;
arbore[i]=i;
}
}
int Find(int x)
{
int rad , y;
for(rad=x ; arbore[rad]!=rad ; rad=arbore[rad])
;
while(arbore[x]!=x)
{
y=arbore[x];
arbore[x]=rad;
x=y;
}
return rad;
}
void Unire(int x, int y)
{
int xRoot=Find(x);
int yRoot=Find(y);
if(xRoot == yRoot)
return;
if(rang[xRoot] < rang[yRoot])
arbore[xRoot] = yRoot;
else
{
arbore[yRoot] = xRoot;
rang[xRoot] = rang[xRoot]+1;
}
}
int main()
{
int x , y , i , op;
fin>>N>>M;
Initiailizare();
for(i = 1 ; i <= M; i++)
{
fin>>op>>x>>y;
if(op == 2)
{
if(Find(x) == Find(y))
fout<<"DA\n";
else fout<<"NU\n";
}
if(op == 1)
{
if (Find(x) == Find(y))
return 0;
else Unire(Find(x),Find(y));
}
}
cout<<"fuck";
return 0;
}