Pagini recente » Cod sursa (job #2970156) | Cod sursa (job #372270) | Arhiva de probleme | Cod sursa (job #683690) | Cod sursa (job #2238278)
#include <fstream>
using namespace std;
const int NMAX=100000;
int t[NMAX+5],h[NMAX+5];
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int FindSet(int x)
{
while(x!=t[x])
x=t[x];
return x;
}
void UnitSet(int x,int y)
{
if(h[x]==h[y]){
h[x]++;
t[y]=x;
}
else if(h[x]>h[y])
t[y]=x;
else t[x]=y;
}
int main()
{
int n,m,i,x,y,tx,ty,c;
in>>n>>m;
for(i=1;i<=n;i++){
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++)
{
in>>c;
in>>x>>y;
tx=FindSet(x);
ty=FindSet(y);
if(c==1)
UnitSet(tx,ty);
else if(c==2)
{
if(tx==ty)
out<<"DA"<<'\n';
else out<<"NU"<<'\n';
}
}
return 0;
}