Pagini recente » Monitorul de evaluare | Cod sursa (job #578329) | Cod sursa (job #563465) | Cod sursa (job #320013) | Cod sursa (job #3309989)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
int n,m,c;
int tata[100005];
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;
else
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(rad(x)==rad(y))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
return 0;
}