Pagini recente » Cod sursa (job #476195) | Cod sursa (job #1191955) | Cod sursa (job #3039513) | Cod sursa (job #2481080) | Cod sursa (job #2915253)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int NMAX=100000;
int t[NMAX+5], h[NMAX+5];
int FindRoot(int x){
while(x!=t[x])
x=t[x];
return x;
}
void UnionSet(int x, int y){
if(h[x]>h[y])
t[y]=x;
else if(h[y]>h[x])
t[x]=y;
else{
t[y]=x;
h[x]++;
}
}
int main(){
int n, m, cer, x, y, tx, ty;
fin>>n>>m;
for(int i=1;i<=n;i++){
t[i]=i;
h[i]=1;
}
for(int i=1;i<=m;++i){
fin>>cer>>x>>y;
tx=FindRoot(x);
ty=FindRoot(y);
if(cer==1 && tx!=ty)
UnionSet(tx, ty);
else{
if(ty==tx)
fout<<"DA\n";
else fout<<"NU\n";
}
}
return 0;
}