Pagini recente » Cod sursa (job #964898) | Cod sursa (job #1708708) | Cod sursa (job #85521) | Cod sursa (job #362569) | Cod sursa (job #2939944)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
vector <int> h, t, nr;
void init() {
h = vector <int> (n + 1);
t = vector <int> (n + 1);
for(int i = 1; i <= n; i ++)
t[i] = i;
}
int getRoot(int node) {
if(node == t[node])
return node;
return (t[node] = getRoot(t[node]));
}
void unite(int x, int y) {
int rootX = getRoot(x);
int rootY = getRoot(y);
if(rootX == rootY)
return;
if(h[rootX] < h[rootY]) {
t[rootY] = rootX;
}
else {
if(h[rootX] == h[rootY])
++h[rootY];
t[rootX] = rootY;
}
}
bool sameConnectedComponents(int x, int y) {
return getRoot(x) == getRoot(y);
}
void solve() {
int x, y, op;
fin >> n >> m;
init();
for(int i = 1; i <= m; i ++) {
fin >> op >> x >> y;
if(op == 1)
unite(x, y);
else
fout << (sameConnectedComponents(x, y) ? "DA\n" : "NU\n");
}
}
int main() {
solve();
return 0;
}