Pagini recente » Cod sursa (job #647658) | Cod sursa (job #795894) | Cod sursa (job #2263444) | Cod sursa (job #1035386) | Cod sursa (job #2946576)
#include <vector>
#include <bitset>
#include <fstream>
using namespace std;
const int N = 1e5 + 1;
int t[N], rang[N];
void Union (int x, int y)
{
if (rang[x] > rang[y])
t[y] = x;
else
{
t[x] = y;
if (rang[x] == rang[y]) rang[y]++;
}
}
int Find(int x)
{
if (t[x] == 0)
return x;
int y = Find(t[x]);
t[x] = y;
return y;
}
int main()
{
int n, queries;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
fin >> n >> queries;
while(queries--)
{
int op, x, y;
fin >> op >> x >> y;
x = Find(x);
y = Find(y);
if (op == 1)
Union(x, y);
else (x == y) ? fout << "DA\n" : fout << "NU\n";
}
fin.close();
fout.close();
}