Pagini recente » Cod sursa (job #1073055) | Cod sursa (job #801292) | Cod sursa (job #3266417) | Cod sursa (job #2319491) | Cod sursa (job #1906068)
#include <cstdio>
using namespace std;
int h[100005];
int t[100005];
int findmd(int x)
{
while(x!=t[x])
x=t[x];
return x;
}
void unionmd(int tx,int ty)
{
if(h[tx]==h[ty])
{
h[tx]++;
t[ty]=t[tx];
}
else
if(h[tx]>h[ty])
t[ty]=t[tx];
else
t[tx]=t[ty];
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m,x,y,op,i,tx,ty;
scanf("%d%d",&n,&m);
for(i=1; i<=n; i++)
h[i]=1,t[i]=i;
for(i=1; i<=m; i++)
{
scanf("%d%d%d",&op,&x,&y);
tx=findmd(x);
ty=findmd(y);
if(op==1)
unionmd(tx,ty);
else
if(tx==ty)
printf("DA\n");
else
printf("NU\n");
}
return 0;
}