Pagini recente » Cod sursa (job #3304098) | Cod sursa (job #3344560) | Borderou de evaluare (job #3303914) | Cod sursa (job #3344111) | Cod sursa (job #3343598)
#include<fstream>
#include<vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX=1e5+2;
int t[NMAX];
int s[NMAX];
int findRoot(int x){
int root=x;
int aux=x;
while(t[root]!=root){
root=t[root];
}
while(x!=root){
aux=t[x];
t[x]=root;
x=aux;
}
return x;
}
int main(){
int n,m;
fin>>n>>m;
for(int i=0;i<NMAX;i++){
t[i]=i;
s[i]=1;
}
for(int i=0;i<m;i++){
int c,x,y;
fin>>c>>x>>y;
if(c==1){
x=findRoot(x);
y=findRoot(y);
if(s[x]>s[y]){
t[y]=x;
s[x]+=s[y];
}else{
t[x]=y;
s[y]+=s[x];
}
}else if(c==2){
if(findRoot(x)==findRoot(y)){
fout<<"DA"<<'\n';
}else{
fout<<"NU"<<'\n';
}
}
}
return 0;
}