Pagini recente » Cod sursa (job #2600224) | Cod sursa (job #1151109) | Cod sursa (job #2314398) | Cod sursa (job #2677228) | Cod sursa (job #2672663)
/**
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],r[nmax];
int n,m;
int radacina(int x)
{
while(vt[x]!=x)
x=vt[x];
return 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;
}