Pagini recente » Cod sursa (job #2473720) | Cod sursa (job #1386220) | Cod sursa (job #130590) | Cod sursa (job #2705049) | Cod sursa (job #1204382)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
const int NMAX = 100000 + 1;
int n, m;
int t[NMAX], r[NMAX];
void initializeaza () {
f >> n >> m;
for (int i = 1; i <= n; i++) t[i] = i, r[i] = 1;
}
int cauta (int x) {
int rd = x, y;
while (rd != t[rd]) rd = t[rd];
while (t[x] != x) {
y = t[x]; t[x] = rd; x = y;
}
return rd;
}
void uneste (int x, int y) {
if (r[x] > r[y]) t[y] = x;
else t[x] = y;
if (r[x] == r[y]) r[y]++;
}
void rezolva () {
int q, x, y;
for (int i = 1; i <= m; i++) {
f >> q >> x >> y;
if (q == 2) {
if(cauta(x) == cauta(y)) g << "DA";
else g << "NU";
g << '\n';
}
else {
uneste(cauta(x), cauta(y));
}
}
}
int main() {
initializeaza();
rezolva();
return 0;
}