Pagini recente » Cod sursa (job #2493022) | Monitorul de evaluare | Cod sursa (job #1389792) | Cod sursa (job #982895) | Cod sursa (job #2861865)
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n, m, t[100001];
int cauta (int x) {
int y = x, aux;
while (y != t[y])
y = t[y];
while (x != y) {
aux = t[x];
t[x] = y;
x = aux;
}
return x;
}
void uneste (int x, int y) {
x = cauta (x);
y = cauta (y);
t[y] = x;
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; i++)
t[i] = i;
for (int i = 1; i <= m; i++) {
int op, x, y;
fin >> op >> x >> y;
if (op == 1)
uneste (x, y);
else {
if (cauta (x) != cauta (y))
fout << "NU\n";
else
fout << "DA\n";
}
}
return 0;
}