Pagini recente » Cod sursa (job #1106042) | Cod sursa (job #1906688) | Cod sursa (job #1075900) | Cod sursa (job #2104184) | Cod sursa (job #1807126)
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int maxn = 100005;
int F[maxn];
int rad(int nod)
{
if(F[nod] < 0)
return nod;
return rad(F[nod]);
}
void add(int x, int y)
{
if(x == y)
return;
F[y] += F[x];
F[x] = y;
}
int main()
{
int n, m;
in >> n >> m;
fill(F + 0, F + maxn, -1);
for(int i = 1; i <= m; i++)
{
int op, x, y;
in >> op >> x >> y;
if(op == 1)
{
int px = rad(x);
int py = rad(y);
int dim1 = -F[px];
int dim2 = -F[py];
if(dim1 < dim2)
add(px, py);
else
add(py, px);
}
else
{
if(rad(x) == rad(y))
out << "DA";
else
out << "NU";
out << "\n";
}
}
return 0;
}