Pagini recente » Cod sursa (job #2402722) | Cod sursa (job #897264) | Cod sursa (job #1992861) | Cod sursa (job #2719357) | Cod sursa (job #2812808)
using namespace std;
#include<bits/stdc++.h>
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define DIM 100001
int repr[DIM], n, m, x, y;
int cauta_repr(int elem) {
if (elem == repr[elem]) {
return elem;
}
repr[elem] = cauta_repr(repr[elem]);
return repr[elem];
}
void unire_triburi(int om1, int om2) {
int repr_om1 = cauta_repr(om1);
int repr_om2 = cauta_repr(om2);
if (repr_om1 != repr_om2) {
repr[repr_om1] = repr_om2;
}
}
int main() {
fin >> n >> m;
for (int i = 1; i<=n; i++) {
repr[i] = i;
}
int tip, x, y;
while (m) {
m--;
fin >> tip >> x >> y;
if (tip == 1) {
unire_triburi(x,y);
} else {
int rx = cauta_repr(x);
int ry = cauta_repr(y);
if (rx == ry) {
fout << "DA \n";
} else {
fout << "NU \n";
}
}
}
return 0;
}