Pagini recente » Cod sursa (job #1344462) | Cod sursa (job #2226849) | Cod sursa (job #594964) | Cod sursa (job #1998638) | Cod sursa (job #2000148)
#include <cstdio>
using namespace std;
const int NMAX=100005;
int t[NMAX];
int h[NMAX];
int FindSet(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
void UnionSet(int x,int y)
{
if(h[x]==h[y])
{
t[y]=x;
h[x]++;
}
else
if(h[x]>h[y])
t[y]=x;
else
t[x]=y;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m,i,tip,x,y;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&tip,&x,&y);
if(tip==1)
{
x=FindSet(x);
y=FindSet(y);
UnionSet(x,y);
}
else
{
if(FindSet(x)==FindSet(y))
printf("DA");
else
printf("NU");
printf("\n");
}
}
return 0;
}