Pagini recente » Cod sursa (job #1458733) | Cod sursa (job #2742679) | Cod sursa (job #2083584) | Cod sursa (job #578560) | Cod sursa (job #3307260)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int MAX=1e5;
int n,i,T[MAX+5],R[MAX+5],type,x,y,q;
int root(int x)
{
if (T[x]==0)
return x;
return T[x]=root(T[x]);
}
void unite(int x, int y)
{
int rx=root(x),ry=root(y);
if (rx==ry)
return;
if (R[rx]>R[ry])
{
R[rx]+=R[ry];
T[ry]=rx;
}
else
{
R[ry]+=R[rx];
T[rx]=ry;
}
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(0); fout.tie(0);
fin>>n>>q;
for (i=1; i<=n; i++)
R[i]=1;
while (q--)
{
fin>>type>>x>>y;
if (type==1)
unite(x,y);
else
{
if (root(x)==root(y))
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
}
return 0;
}