Pagini recente » Cod sursa (job #719820) | Cod sursa (job #1554708) | Cod sursa (job #2467084) | Cod sursa (job #252815) | Cod sursa (job #2513613)
#include <fstream>
#include <vector>
#define MAXV 100001
using namespace std;
vector<int> p (MAXV);
vector<int> s (MAXV, 1);
int root(int x) {
int aux = x;
while (p[x] !=x) {
x = p[x];
}
while (p[aux] != aux) {
int next = p[aux];
p[aux] = x;
aux = next;
}
return x;
}
void uni(int x, int y) {
int rx = root(x), ry = root(y);
if (s[rx] >= s[ry]) {
p[ry] = rx;
s[rx] += s[ry];
} else {
p[rx] = ry;
s[ry] += s[rx];
}
}
bool find(int x, int y) {
return root(x) == root(y);
}
int main() {
ifstream in ("disjoint.in");
ofstream out("disjoint.out");
int n, m;
in >> n >> m;
for (int i = 1; i <= n; ++i)
p[i] = i;
int cod, x, y;
for (int i = 0; i < m; ++i) {
in >> cod >> x >> y;
if (cod == 1) {
uni(x, y);
} else {
if (find(x, y)) out << "DA\n";
else out << "NU\n";
}
}
}