Pagini recente » Cod sursa (job #1500600) | Cod sursa (job #2139799) | Cod sursa (job #3255634) | Cod sursa (job #2608292) | Cod sursa (job #2941314)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
vector<int> tata;
int radacina(int x) {
int r = x;
while (tata[r] != r) {
r = tata[r];
}
while (tata[x] != x) {
int aux = tata[x];
tata[x] = r;
x = aux;
}
return r;
}
int main() {
fin >> n >> m;
tata.resize(n + 1);
for (int i = 1; i <= n; i++) {
tata[i] = i;
}
for (int i = 0; i < m; i++) {
int cod, x, y;
fin >> cod >> x >> y;
x = radacina(x);
y = radacina(y);
if (cod == 1) {
tata[y] = x;
}
else {
if (x == y)
fout << "DA" << endl;
else
fout << "NU" << endl;
}
}
}