Pagini recente » Cod sursa (job #1806668) | Cod sursa (job #1399876) | Cod sursa (job #2643389) | Cod sursa (job #1412147) | Cod sursa (job #658185)
Cod sursa(job #658185)
#include<cstdio>
#define NMAX 100001
using namespace std;
long n,m;
long t[NMAX];
long r[NMAX];
void read () {
scanf("%ld%ld",&n,&m);
for (long i=1;i<=n;i++) {
t[i]=i;
r[i]=1;
}
}
void unite (long Rx , long Ry) {
if (r[Rx]>r[Ry])
t[Ry]=Rx;
else
t[Rx]=Ry;
if (r[Rx]==r[Ry])
r[Ry]++;
}
long radacina (long nod) {
long R,aux;
for (R=nod;t[R]!=R;R=t[R]);
while (t[nod]!=nod) {
aux=t[nod];
t[nod]=R;
nod=aux;
}
return R;
}
void solve () {
long x,y,tip;
for (long i=1;i<=m;i++) {
scanf("%ld%ld%ld",&tip,&x,&y);
if (tip==1)
unite(radacina(x),radacina(y));
else {
if (radacina(x)==radacina(y))
printf("DA\n");
else
printf("NU\n");
}
}
}
int main () {
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
read ();
solve ();
return 0;
}