Pagini recente » Cod sursa (job #3309230) | Cod sursa (job #3304175) | Cod sursa (job #3307210) | Cod sursa (job #3344114) | Cod sursa (job #3343613)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int dim[100009], repr[100009];
int get_repr (int x)
{
if (x==repr[x])
return x;
repr[x]=get_repr (repr[x]);
return repr[x];
}
void join (int x, int y)
{
x=get_repr(x), y=get_repr(y);
if (x==y) return;
if (dim[x]<dim[y])
swap (x, y);
dim[x]+=dim[y];
repr[y]=repr[x];
}
signed main ()
{
int n;
f >> n;
for (int i=1; i<=n; i++)
dim[i]=1, repr[i]=i;
int q;
f >> q;
while (q--)
{
int tip, x, y;
f >> tip >> x >> y;
if (tip==1)
join (x, y);
else
{
x=get_repr(x), y=get_repr(y);
if (x==y)
g <<"DA\n";
else
g << "NU\n";
}
}
}