Pagini recente » Borderou de evaluare (job #2473485) | Cod sursa (job #1464610) | Cod sursa (job #2781504) | Cod sursa (job #2121659) | Cod sursa (job #2640305)
#include <iostream>
#include <fstream>
using namespace std;
int multime[100001], h[100001];
int getSet(int x) {
int result = x;
while (result != multime[result]) {
result = multime[result];
}
while (x != result) {
int nextX = multime[x];
multime[x] = result;
x = nextX;
}
return result;
}
void unite(int x, int y) {
int mX = getSet(x);
int mY = getSet(y);
if (mX == mY) return;
if (h[mX] > h[mY]) {
multime[mY] = mX;
} else if (h[mY] > h[mX]) {
multime[mX] = mY;
} else {
multime[mY] = mX;
h[mX] += 1;
}
}
int main()
{
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
fin >> n >> m;
for (int i = 1; i <= n; ++i) {
multime[i] = i;
h[i] = 1;
}
for (int i = 1; i <= m; ++i) {
int type, x, y;
fin >> type >> x >> y;
if (type == 1) {
unite(x, y);
} else {
if (getSet(x) == getSet(y)) {
fout << "DA\n";
} else {
fout << "NU\n";
}
}
}
return 0;
}