Pagini recente » Cod sursa (job #835299) | Cod sursa (job #2862994) | Cod sursa (job #1690721) | Cod sursa (job #2775234) | Cod sursa (job #2488748)
#include <iostream>
#include <fstream>
#define NMAX 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int rad[NMAX], h[NMAX];
int N,M;
int FindRoot(int x)
{
while(rad[x] != x)
x = rad[x];
return x;
}
void Union(int x, int y)
{
// x,y sunt radacini
if(h[x] > h[y])
rad[y] = x;
else
{
if(h[y] > h[x])
rad[x] = y;
else
{
rad[y] = x;
h[x]++;
}
}
}
int main()
{
int type, tx, ty, x, y;
fin >> N >> M;
for(int i = 1; i <= N; i++)
rad[i] = i, h[i] = 1;
for(int i = 1; i <= M; i++)
{
fin >> type >> tx >> ty;
if(type == 1)
{
x = FindRoot(tx);
y = FindRoot(ty);
Union(x,y);
}
else
{
x = FindRoot(tx);
y = FindRoot(ty);
if(x == y)
fout << "DA" << "\n";
else
fout << "NU" << "\n";
}
}
return 0;
}