Pagini recente » Cod sursa (job #661049) | Cod sursa (job #890237) | Cod sursa (job #707375) | Cod sursa (job #1628202) | Cod sursa (job #2934654)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n, m, cod, x, y;
vector<int> parent, height;
int rep(int x) {
if (!parent[x])
return x;
int reprez = rep(parent[x]);
parent[x] = reprez;
return reprez;
}
void myUnion (int x, int y) {
int repx = rep(x), repy = rep(y);
if (height[repx] < height[repy])
parent[repx] = repy;
else if (height[repy] < height[repx])
parent[repy] = repx;
else {
parent[repx] = repy;
++ height[repy];
}
}
int main() {
in >> n >> m;
parent.resize(n + 1, 0);
height.resize(n + 1, 0);
while (m --) {
in >> cod >> x >> y;
if (cod == 1)
myUnion(x, y);
else
out << ((rep(x) == rep(y))? "DA\n" : "NU\n");
}
return 0;
}