Pagini recente » Cod sursa (job #3216853) | Cod sursa (job #1503408) | Cod sursa (job #2792884) | Cod sursa (job #646518) | Cod sursa (job #2940970)
#include <fstream>
#include <iostream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m,cod,x,y,i,tata[100001],grad[100001],aux,t1,t2;
int caut(int t) {
aux=t;
while(tata[t]!=t)
t=tata[t];
while(aux!=t) {
tata[aux]=t;
aux=tata[aux];
}
return t;
}
void op1(int x, int y) {
t1=caut(x);
t2=caut(y);
if(grad[t1]>grad[t2])
tata[t2]=t1;
else
tata[t1]=t2;
// if(grad[t1]==grad[t2])
// grad[t2]++;
}
void op2(int x, int y) {
if(caut(x)==caut(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
int main(){
fin>>n>>m;
for(i=1;i<=n;i++){
tata[i]=i;
grad[i]=1;
}
for(i=0;i<m;i++){
fin>>cod>>x>>y;
if(cod==1)
op1(x,y);
else
op2(x,y);
}
return 0;
}