Pagini recente » Cod sursa (job #2414293) | Cod sursa (job #855563) | Cod sursa (job #2685521) | Cod sursa (job #1463351) | Cod sursa (job #2837386)
#include <fstream>
#include <unordered_map>
#include <map>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
unordered_map<int, int> adj;
int n, x, y, afirm, m;
int radacina(int a) {
if (a == adj[a]) {
return a;
}
else {
return adj[a] = radacina(adj[a]);
}
}
int main()
{
cin >> m >> n;
for (int i = 1; i <= n; i++) {
cin >> afirm;
cin >> x >> y;
if (afirm == 1) {
if (adj[x] == 0 && adj[y] == 0) {
adj[x] = x;
adj[y] = x;
}
else if (adj[x] == 0 && adj[y] != 0) {
adj[x] = adj[y];
}
else if (adj[x] != 0 && adj[y] == 0) {
adj[y] = adj[x];
}
else {
int interm1 = radacina(x);
int interm2 = radacina(y);
if (interm1 > interm2) {
adj[interm1] = adj[interm2];
}
else {
adj[interm2] = adj[interm1];
}
}
}
else {
int interm1 = radacina(x);
int interm2 = radacina(y);
if (interm1 == interm2 && interm1 != 0 && interm2 != 0) {
cout << "DA" << '\n';
}
else {
cout << "NU" << '\n';
}
}
}
}