Pagini recente » Cod sursa (job #3323997) | Cod sursa (job #1219079) | Cod sursa (job #481207) | Cod sursa (job #1678741) | Cod sursa (job #3309987)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
int n,m,c;
int tata[100002];
int rad(int x){
if(tata[x]==x){
return x;
}
else{
tata[x]=rad(tata[x]);
return rad(tata[x]);
}
}
void unite(int x,int y){
int rx=rad(x);
int ry=rad(y);
if(rx<=ry){
tata[y]=x;
}
else{
tata[x]=y;
}
}
bool check(int x,int y){
int rx=rad(x);
int ry=rad(y);
if(rx==ry)
return true;
return false;
}
int main(){
in>>n>>m;
for(int i=1;i<=n;i++){
tata[i]=i;
}
for(int i=1;i<=m;i++){
in>>c;
int x,y;
in>>x>>y;
if(c==1){
unite(x,y);
}
else{
if(check(x,y)==true)
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
return 0;
}