Pagini recente » Cod sursa (job #676311) | Cod sursa (job #1045292) | Cod sursa (job #1884452) | Cod sursa (job #1931580) | Cod sursa (job #2914453)
#include <bits/stdc++.h>
#define MAXN 100000
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, id[MAXN], sz[MAXN];
int root(int x) {
while (id[x] != x) {
id[x] = id[id[x]];
x = id[x];
}
return x;
}
void connect(int x, int y) {
int rootX = root(x), rootY = root(y);
if (sz[rootX] < sz[rootY]) {
id[rootX] = rootY;
sz[rootY] += sz[rootX];
} else {
id[rootY] = rootX;
sz[rootX] += sz[rootY];
}
}
bool isConnected(int x, int y) {
return root(x) == root(y);
}
int main() {
int n, m;
fin >> n >> m;
// initializare
for (int i = 0; i < n; i++) {
id[i] = i;
sz[i] = 1;
}
for (int i = 0; i < m; i++) {
int cod, x, y;
fin >> cod >> x >> y;
switch (cod) {
case 1:
connect(x, y);
break;
case 2:
fout << (isConnected(x, y) ? "DA" : "NU") << '\n';
break;
}
}
return 0;
}