Pagini recente » Cod sursa (job #2663397) | Cod sursa (job #921433) | Cod sursa (job #2960673) | Cod sursa (job #2030816) | Cod sursa (job #2496277)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int tata[100001];
int sef(int x)
{
if(x==tata[x])return x;
else return tata[x]=sef(tata[x]);
}
void unire(int x,int y)
{
int sefx,sefy;
sefx=sef(x);
sefy=sef(y);
tata[sef(x)]=sefy;
}
int main()
{
int n,m,cod,x,y,i;
in>>n>>m;
for(i=1;i<=n;i++)tata[i]=i;
for(i=1;i<=m;i++)
{
in>>cod>>x>>y;
if(cod==1)
{
unire(x,y);
}
else if(cod==2)
{
if(sef(x)==sef(y))out<<"DA"<<'\n';
else out<<"NU"<<'\n';
}
}
return 0;
}