Pagini recente » Cod sursa (job #2836939) | Cod sursa (job #2697208) | Cod sursa (job #1681226) | Cod sursa (job #2690566) | Cod sursa (job #1495266)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
const int NMAX = 100000;
int n, m;
int father[NMAX], height[NMAX];
void read() {
f >> n >> m;
}
void init() {
for (int i = 1; i <= n; i++) {
father[i] = i;
height[i] = 1;
}
}
int root(int x) {
if (father[x] == x) return x;
father[x] = root(father[x]);
return father[x];
}
void unite(int a, int b) {
a = root(a);
b = root(b);
if (height[b] > height[a]) father[a] = b;
else father[b] = a;
if (height[a] == height[b]) height[a]++;
}
bool are_united(int a, int b) {
return root(a) == root(b);
}
void solve() {
int type, a, b;
for (int i = 1; i <= m; i++) {
f >> type >> a >> b;
if (type == 1) unite(a, b);
else {
if (are_united(a, b)) g << "DA\n";
else g << "NU\n";
}
}
}
int main() {
read();
init();
solve();
return 0;
}