Pagini recente » Cod sursa (job #819171) | Cod sursa (job #613308) | Cod sursa (job #1583822) | Cod sursa (job #548911) | Cod sursa (job #2566382)
#include <fstream>
#include <iostream>
#include <stack>
using namespace std;
const int N = 1e5 + 5;
int f[N], n;
inline void Union (int x, int y)
{
f[x] = y;
}
int Find (int x)
{
int root, y;
root = x;
while (f[root])
root = f[root];
while (x != root)
{
y = f[x];
f[x] = root;
x = y;
}
return root;
}
void Read ()
{
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int q;
fin >> n >> q;
while (q--)
{
int op, x, y;
fin >> op >> x >> y;
x = Find(x);
y = Find(y);
if (op == 1)
{
if (x != y)
Union(x, y);
}
else
{
if (x == y) fout << "DA\n";
else fout << "NU\n";
}
}
fout.close();
fin.close();
}
int main()
{
Read();
return 0;
}