Pagini recente » Cod sursa (job #2749452) | Cod sursa (job #260215) | Cod sursa (job #635886) | Cod sursa (job #2504688) | Cod sursa (job #2173169)
#include <iostream>
#include <fstream>
#define DM 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, a, b, c, dad[DM];
int root(int nod)
{
if(dad[nod] == nod)
return nod;
return dad[nod] = root(dad[nod]);
}
void unite(int x, int y)
{
dad[root(x)] = root(y);
}
int main ()
{
fin >> n >> m;
for(int i = 1; i <= n; i++)
dad[i] = i;
for(int i = 1; i <= m; i++)
{
fin >> a >> b >> c;
if(a == 1)
unite(b, c);
if(a == 2)
{
if(root(b) == root(c))
fout << "DA" << '\n';
else fout << "NU" << '\n';
}
}
return 0;
}