Pagini recente » Cod sursa (job #1746875) | Cod sursa (job #63617) | Cod sursa (job #1117853) | Cod sursa (job #1287540) | Cod sursa (job #2134192)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
int Get_Root (int x, vector < int > &root) {
int r = x;
while (r != root[r]) {
r = root[r];
}
while (x != root[x]) {
int aux = root[x];
root[x] = r;
x = aux;
}
return r;
}
void Link (int x, int y, vector < int > &root, vector < int > &rang) {
x = Get_Root (x, root);
y = Get_Root (y, root);
if (rang[x] == rang[y]) {
++ rang[x];
}
else if (rang[x] < rang[y]) {
swap (x, y);
}
root[y] = x;
}
int main () {
int n, m;
cin >> n >> m;
vector < int > root (n + 1);
vector < int > rang (n + 1);
for (int i = 1; i <= n; ++ i) {
root[i] = i;
rang[i] = 1;
}
while (m --) {
int type;
cin >> type;
if (type == 1) {
int x, y;
cin >> x >> y;
Link (x, y, root, rang);
}
else {
int x, y;
cin >> x >> y;
if (Get_Root (x, root) == Get_Root (y, root)) {
cout << "DA\n";
}
else {
cout << "NU\n";
}
}
}
}