Pagini recente » Cod sursa (job #980310) | Cod sursa (job #2463465) | Cod sursa (job #2085723) | Cod sursa (job #2046801) | Cod sursa (job #3215169)
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int NMAX = 1e5;
int father[NMAX + 5];
int root(int node)
{
if(father[node] == node)
return node;
return node = root(father[node]);
}
void Union(int node1, int node2)
{
father[root(node1)] = father[root(node2)];
}
int main()
{
int n, m;
fin >> n >> m;
for(int i = 1; i <= n; i++)
father[i] = i;
for(int i = 1; i <= m; i++)
{
int op, x, y;
fin >> op >> x >> y;
if(op == 1)
Union(x, y);
else if(op == 2)
{
if(root(x) == root(y))
fout << "DA\n";
else fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}