Pagini recente » Cod sursa (job #821028) | Cod sursa (job #2298658) | Cod sursa (job #473422) | Cod sursa (job #1354497) | Cod sursa (job #2672666)
/**
infoarena.ro/problema/disjoint
Paduri de multimi disjuncte
*/
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int nmax=100005;
int vt[nmax];
int n,m;
int radacina(int x)
{
if(vt[x]!=x)
vt[x]=radacina(vt[x]);
return vt[x];
}
void unire(int x,int y)
{
if(x<y)
vt[y]=x;
if(x>y)
vt[x]=y;
/**if(x==y)
{
vt[x]=y;
x++;
}*/
}
int main()
{
fin>>n>>m;
for(int i=1;i<=n;i++)
vt[i]=i;
int t,x,y;
while(m--)
{
fin>>t>>x>>y;
int a=radacina(x);
int b=radacina(y);
if(t==1)
unire(a,b);
if(t==2)
if(a==b)
fout<<"DA"<<endl;
else
fout<<"NU"<<endl;
}
return 0;
}