Pagini recente » Cod sursa (job #1781942) | Cod sursa (job #2497051) | Cod sursa (job #527033) | Cod sursa (job #952398) | Cod sursa (job #1974633)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 100000 + 5;
int n, m, x, y, cod;
int rang[NMAX], root[NMAX];
void read()
{
fin >> n >> m;
for (int i = 1; i <= n; ++i)
{
root[i] = i;
rang[i] = 1;
}
}
int find_root(int x)
{
int i, aux;
for (i = x; root[i] != i; i = root[i]);
while (root[x] != x)
{
aux = root[x];
root[x] = i;
x = aux;
}
return i;
}
void unite(int x, int y)
{
if (rang[x] > rang[y])
root[y] = x;
else
root[x] = y;
if (rang[x] == rang[y])
++rang[y];
}
int main()
{
read();
for (int i = 1; i <= m; ++i)
{
fin >> cod >> x >> y;
if (cod == 2)
{
if (find_root(x) == find_root(y))
fout << "DA\n";
else
fout << "NU\n";
}
else
{
if (find_root(x) != find_root(y))
unite(find_root(x), find_root(y));
}
}
return 0;
}