Pagini recente » Cod sursa (job #2444069) | Cod sursa (job #3225487) | Cod sursa (job #884670) | Monitorul de evaluare | Cod sursa (job #1999504)
#include <cstdio>
using namespace std;
int p[100005], h[100005];
int FindSet(int x)
{
while(p[x]!=x)
x=p[x];
return x;
}
void UnionSet(int x, int y)
{
if (h[x]>=h[y])
{
p[y]=x;
h[x]++;
}
else
{
p[x]=y;
h[y]++;
}
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int n, m, i, u, v, tu, tv, cod;
scanf("%d%d", &n, &m);
for(i=1; i<=n; i++)
p[i]=i, h[i]=1;
for(i=1; i<=m; i++)
{
scanf("%d%d%d", &cod, &u, &v);
tu=FindSet(u);
tv=FindSet(v);
if (cod==1)
UnionSet(tu, tv);
else
if (tu==tv)
printf("DA\n");
else
printf("NU\n");
}
return 0;
}