Pagini recente » Cod sursa (job #3142489) | Cod sursa (job #2587660) | Cod sursa (job #840934) | Cod sursa (job #2873514) | Cod sursa (job #1533252)
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
const int nmax = 100002 ;
int n,m, father[nmax], hight[nmax];
void unire(int a, int b);
bool sameGroup(int a, int b);
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d %d", &n, &m);
memset(father,0,4*(n+1));
memset(hight,0,4*(n+1));
int a,b,c;
for(;m--;) {
scanf("%d %d %d", &a, &b, &c);
if (a == 1 ) {
unire(b,c);
}
else {
if (sameGroup(b,c)) printf("DA\n");
else printf("NU\n");
}
}
fclose(stdin);
fclose(stdout);
return 0;
}
void unire(int a, int b) {
int ha = 0, hb = 0, root;
int aa = a, bb = b ,x;
while (father[aa] != 0) { aa = father[aa]; ha++ ;}
while (father[bb] != 0) { bb = father[bb]; hb++ ;}
if (ha < hb) {root = bb; father[a] = root; }
else {root = aa; father[b] = root; }
aa = a; bb = b;
while (father[aa] != 0) { x = father[aa]; father[aa] = root; aa = x;}
while (father[bb] != 0) { x = father[bb]; father[bb] = root; bb = x;}
}
bool sameGroup(int a, int b) {
while (father[a] != 0) a = father[a];
while (father[b] != 0) b = father[b];
return a==b;
}