Pagini recente » Istoria paginii utilizator/samoila_alexandru | Profil StarGold2 | Cod sursa (job #2724853) | Cod sursa (job #1992669) | Cod sursa (job #1016056)
#include <cstdio>
using namespace std;
int t[100010], h[100010];
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[y]=x;
else t[x]=y;
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int n, m, x, y, tip;
scanf("%d%d", &n, &m);
for(int i=1;i<=n;i++)
t[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d", &tip, &x, &y);
if(tip==1)
unionset(t[x], t[y]);
else
{
if(findset(x)==findset(y))
printf("DA\n");
else printf("NU\n");
}
}
return 0;
}