Pagini recente » Cod sursa (job #2473712) | Cod sursa (job #2566864) | Cod sursa (job #1102676) | Cod sursa (job #678997) | Cod sursa (job #1243726)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
const int NMAX = 100000 + 1;
int n, q;
int h[NMAX], t[NMAX];
void uneste(int x, int y) {
if (h[x] < h[y]) t[x] = y;
else t[y] = x;
if (h[x] == t[y]) h[x]++;
}
int radacina(int x) {
while (t[x] != x) x = t[x];
return x;
}
void rezolva() {
int tip, x, y, rx, ry;
while(q--) {
f >> tip >> x >> y;
rx = radacina(x);
ry = radacina(y);
if (tip == 1) uneste(rx, ry);
else {
if (rx == ry) g << "DA\n";
else g << "NU\n";
}
}
}
void initializeaza() {
for (int i = 1; i <= n; i++) {
t[i] = i;
h[i] = 1;
}
}
int main() {
f >> n >> q;
initializeaza();
rezolva();
return 0;
}