Pagini recente » Istoria paginii utilizator/lawrentiu | Cod sursa (job #1457327) | Cod sursa (job #150182) | Cod sursa (job #991373) | Cod sursa (job #1809837)
#include <iostream>
#include <fstream>
using namespace std;
const int cmax = 1e5 + 1;
ifstream in ("disjoint.in");
ofstream out("disjoint.out");
int t[cmax], r[cmax];
int cautare(int x){
int rad = x;
while(t[rad] != 0)
rad = t[rad];
int tmp;
while(t[x] != 0){
tmp = t[x];
t[x] = rad;
x = tmp;
}
return rad;
}
void uniune(int x, int y){
if(r[x] > r[y])
t[y] = x;
else if(r[x] < r[y]) t[x] = y;
else {t[y] = x; r[x]++;}
}
int main()
{
int cod, x, y, n, m;
in >> n >> m;
for(int i = 1; i <= m; i++){
in >> cod >> x >> y;
if(cod == 1) uniune(cautare(x), cautare(y));
else {
if(cautare(x) == cautare(y)) out << "DA\n";
else out << "NU\n";
}
}
return 0;
}