Pagini recente » Cod sursa (job #2441806) | Cod sursa (job #3316929) | Cod sursa (job #3316947) | Cod sursa (job #1015964) | Cod sursa (job #2846940)
#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)
{
if(t[x] != x)
return Find(t[x]);
else
return x;
}
int main()
{
int op, x, y, r1, r2;
fin >> n >> m;
for(int i = 1; i <= n; i++)
t[i] = i;
for(int i = 1; i <= m; i++)
{
fin >> op >> x >> y;
if(op == 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";
}
}
}
return 0;
}