Pagini recente » Cod sursa (job #754062) | Cod sursa (job #2821162) | Cod sursa (job #1906371) | Cod sursa (job #3176241) | Cod sursa (job #1039139)
#include<cstdio>
using namespace std;
int t[100005],h[100005];
int findset(int x)
{
while(t[x]!=x)
{
x=t[x];
}
return x;
}
void unionset(int x, int y)
{
if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else
{
if(h[x]<h[y])
{
t[x]=y;
}
else
{
t[y]=x;
}
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m,a,x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
t[i]=i;
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&x,&y);
if(a==1)
{
unionset(findset(x),findset(y));
}
if(a==2)
{
if(findset(x)==findset(y))
{
printf("DA\n");
}
else
{
printf("NU\n");
}
}
}
}