Pagini recente » Cod sursa (job #2078783) | Cod sursa (job #3129568) | Cod sursa (job #578214) | Monitorul de evaluare | Cod sursa (job #2000134)
#include <cstdio>
using namespace std;
const int NMAX =100005;
int n;
int t[NMAX] , 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 m , cod , x , y ;
scanf("%d%d",&n,&m);
for(int i = 1 ; i <= n ; i++)
{
t[i] = i;
h[i] = 1;
}
for(int i = 1 ; i <= m ; i++)
{
scanf("%d%d%d",&cod,&x,&y);
if(cod == 1)
{
UnionSet(x,y);
}
else
{
int ans1 = FindSet(x);
int ans2 = FindSet(y);
if(ans1 == ans2)
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}