Pagini recente » Cod sursa (job #635907) | Cod sursa (job #2954804) | Cod sursa (job #634288) | Cod sursa (job #2626675) | Cod sursa (job #1380269)
#include <fstream>
using namespace std;
const int NMax = 100005;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N, M;
int T[NMax], Height[NMax];
int Root(int x)
{
int rOOT, aux;
while(x != T[x])
x = T[x];
rOOT = x;
while(x != T[x])
{
aux = T[x];
T[x] = rOOT;
x = aux;
}
return rOOT;
}
void Unite(int x, int y)
{
if(Height[x] > Height[y])
T[y] = x;
else
T[x] = y;
if(Height[x] == Height[y]) Height[y] += 1; //If there're equal, we just add one more
}
void Read()
{
int op, x, y;
fin >> N >> M;
for(int i = 1; i <= N; i++)
{
T[i] = i;
Height[i] = 1;
}
for(int i = 1; i <= M; i++)
{
fin >> op >> x >> y;
if(op == 1)
Unite(Root(x),Root(y));
if(op==2)
{
if(Root(x)==Root(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
}
}
int main()
{
Read();
return 0;
}