Pagini recente » Cod sursa (job #678599) | Cod sursa (job #2290194) | Cod sursa (job #876011) | Cod sursa (job #2773908) | Cod sursa (job #2429626)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
int t[100005],h[100005];
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 FindSet(int x){
while(t[x]!=x)
x=t[x];
return x;
}
int main()
{
int x,y,n,m,tx,ty,cod,i;
in>>n>>m;
for(i=1;i<=n;i++){
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++){
in>>cod>>x>>y;
tx=FindSet(x);
ty=FindSet(y);
if(cod==2){
if(tx==ty)
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
else
if(tx!=ty)
UnionSet(tx,ty);
}
return 0;
}