Pagini recente » Cod sursa (job #2892393) | Cod sursa (job #1851177) | Cod sursa (job #1637002) | Cod sursa (job #1113704) | Cod sursa (job #1217283)
#include <fstream>
#define IN "disjoint.in"
#define OUT "disjoint.out"
using namespace std;
const int MAX=100014;
int rang[MAX],tata[MAX];
ifstream fin(IN);
ofstream fout(OUT);
int stramos(int nod);
void unire(int nod1, int nod2);
int main()
{
int n,m,x,y,z;
fin>>n>>m;
for(register int i=1;i<=n;i++){
tata[i]=i;
rang[i]=1;
}
while(m--){
fin>>z>>x>>y;
if(z==2){
if(stramos(y)==stramos(x))
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
else{
x=stramos(x);
y=stramos(y);
unire(x,y);
}
}
return 0;
}
int stramos(int nod){
int x=nod;
while(nod!=tata[nod])
nod=tata[nod];
while(x!=tata[x]){
x=tata[x];
tata[x]=nod;
}
return nod;
}
void unire(int nod1, int nod2){
if(rang[nod1]>=rang[nod2]){
tata[nod2]=nod1;
rang[nod1]+=rang[nod2];
}
else{
tata[nod1]=nod2;
rang[nod2]+=rang[nod1];
}
}