Pagini recente » Cod sursa (job #2807445) | Cod sursa (job #2182720) | Cod sursa (job #343614) | Cod sursa (job #2045198) | Cod sursa (job #3216044)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m,tata[1000001];
int root(int x){
while(tata[x]>0)
x=tata[x];
return x;
}
void unite(int x,int y){
if(-tata[x]<-tata[y])
swap(x,y);
tata[x]+=tata[y];
tata[y]=x;
}
int main(){
fin>>n>>m;
for(int i=1;i<=n;i++)
tata[i]=-1;
while(m--){
int op;
fin>>op;
if(op==1){
int x,y; fin>>x>>y;
x = root(x);
y = root(y);
if(x!=y)
unite(x,y);
}
else{
int x,y; fin>>x>>y;
x = root(x);
y = root(y);
if(x==y)
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
}
return 0;
}