Pagini recente » Cod sursa (job #1351204) | Cod sursa (job #2303195) | Cod sursa (job #470214) | Cod sursa (job #2744787) | Cod sursa (job #2865226)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, i, j, t, op, roots[100001], sizefor[100001], x, y;
int root (int a)
{
int as = a;
while (a != roots[a])
a = roots[a];
roots[as] = a;
return a;
}
bool join (int x, int y)
{
x = root(x);
y = root(y);
if (sizefor[x] > sizefor[y]) swap(x, y);
roots[x] = y;
sizefor[y] += sizefor[x];
}
int main()
{
fin >> n >> m;
for (i = 1; i<= n; i++)
roots[i] = i, sizefor[i] = 1;
for (t = 1; t <= m; t++)
{
fin >> op >> x >> y;
if (op == 1)
join(x, y);
else if (op == 2)
{
if (root(x) == root(y))
fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}