Pagini recente » Cod sursa (job #1877545) | Cod sursa (job #664162) | Cod sursa (job #580980) | Cod sursa (job #1619707) | Cod sursa (job #2837372)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n, m;
struct hey {
int c;
int x;
int y;
};
int root[100002];
vector <int> vrez;
int radacina(int a) {
if (a == root[a]) {
return a;
}
else {
return root[a] = radacina(root[a]);
}
}
int main()
{
cin >> n >> m;
int c, x, y;
int aux = n, cnt = 0;
root[0] = 0;
for (int i = 1; i <= m; i++) {
cin >> c >> x >> y;
if (c == 1) {
if (root[x] == 0 && root[y] == 0) {
root[x] = x;
root[y] = x;
aux--;
}
else if (root[x] == 0 && root[y] != 0) {
root[x] = root[y];
aux--;
}
else if (root[x] != 0 && root[y] == 0) {
root[y] = root[x];
aux--;
}
else {
if (root[x] == root[y]) {
}
else {
int interm1 = radacina(x);
int interm2 = radacina(y);
if (interm1 == interm2) {
}
else {
aux--;
if (interm2 > interm1) {
root[interm2] = root[interm1];
}
else {
root[interm1] = root[interm2];
}
}
}
}
}
else {
int interm1 = radacina(x);
int interm2 = radacina(y);
if (interm1 == interm2 && interm1 != 0) {
vrez.emplace_back(1);
}
else {
vrez.emplace_back(0);
}
}
}
for (int i = 0; i < vrez.size(); i++) {
if (vrez[i] == 0) {
cout << "NU" << '\n';
}
else {
cout << "DA" << '\n';
}
}
}