Pagini recente » Cod sursa (job #2634963) | Cod sursa (job #673936) | Statistici octav d (petrepetre) | Cod sursa (job #1912077) | Cod sursa (job #1955743)
#include <fstream>
#include <cstdio>
#define NMAX 100005
using namespace std;
ofstream cout("disjoint.out");
int tata[NMAX],H[NMAX];
int n,m,i;
inline void Union(const int &x, const int &y){
if(H[x]>H[y])
tata[y]=x;
else
if(H[x]<=H[y])
tata[x]=y;
}
inline int Find(const int &x){
int rx,x2,aux;
rx=x;
x2=x;
while(tata[rx]) rx=tata[rx];
if(x!=rx)
while(tata[x2]!=rx){
aux=tata[x2];
tata[x2]=rx;
x2=aux;
}
return rx;
}
int main()
{
int x,y,caz,rx,ry;
FILE*fin=freopen("disjoint.in","r",stdin);
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++){
scanf("%d%d%d",&caz,&x,&y);
rx=Find(x);
ry=Find(y);
if(caz==1) Union(rx,ry);
else{
if(rx==ry)
cout<<"DA"<<'\n';
else
cout<<"NU\n";
}
}
return 0;
}