Pagini recente » Cod sursa (job #119771) | Cod sursa (job #450358) | Cod sursa (job #87554) | Cod sursa (job #1139301) | Cod sursa (job #2812806)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define DIM 100002
int repr[DIM];
int cauta_reprez(int elem) {
if(elem == repr[elem]) {
return elem;
}
repr[elem] = cauta_reprez(repr[elem]);
return repr[elem];
}
void unire_triburi(int om1, int om2) {
int repr_om1 = cauta_reprez(om1);
int repr_om2 = cauta_reprez(om2);
if(repr_om1 != repr_om2)
repr[repr_om1] = repr_om2;
}
int main() {
int N, M;
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_reprez(x);
int ry = cauta_reprez(y);
if(rx == ry) fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}