Pagini recente » Cod sursa (job #1115238) | Cod sursa (job #1995761) | Cod sursa (job #874060) | Cod sursa (job #3143985) | Cod sursa (job #2870894)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
int t[100005];
void Union(int x, int y)
{
t[x] = y;
}
int Find(int x)
{
int radacina = x, aux;
while(t[radacina] > 0)
{
radacina = t[radacina];
}
while(t[x] > 0)
{
aux = t[x];
t[x] = radacina;
x = aux;
}
return radacina;
}
void citire()
{
int x, y, z, r1, r2;
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> z >> x >> y;
if(z == 1)
{
r1 = Find(x);
r2 = Find(y);
if(r1 != r2)
{
Union(r1, r2);
}
}
else
{
r1 = Find(x);
r2 = Find(y);
if(r1 != r2)
{
fout << "NU" << "\n";
}
else
{
fout << "DA" << "\n";
}
}
}
}
int main()
{
citire();
return 0;
}