Pagini recente » Cod sursa (job #767294) | Cod sursa (job #2758825) | Cod sursa (job #1288977) | Cod sursa (job #880398) | Cod sursa (job #1669037)
#include <cstdio>
using namespace std;
int t[100001],h[100001];
int FindSet(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
{
h[x]++;
t[y]=x;
}
}
int main()
{
FILE*fin,*fout;
fin=fopen("disjoint.in","r");
fout=fopen("disjoint.out","w");
int n,m,x,y,cod,tx,ty,i;
fscanf(fin,"%d%d",&n,&m);
for(i=1;i<=n;i++)
t[i]=i;
for(i=1;i<=m;i++)
{
fscanf(fin,"%d%d%d",&cod,&x,&y);
if(cod==1)
{
tx=FindSet(x);
ty=FindSet(y);
if(tx!=ty)
UnionSet(tx,ty);
}
else
{
tx=FindSet(x);
ty=FindSet(y);
if(tx==ty)
fprintf(fout,"DA\n");
else
fprintf(fout,"NU\n");
}
}
return 0;
}