Pagini recente » Cod sursa (job #492915) | Cod sursa (job #1969573) | Cod sursa (job #2559474)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int N = 100001;
int t[N], nr[N];
int radacina(int x)
{
if(t[x] == 0){
return x;
}
t[x] = radacina(t[x]);
return t[x];
}
bool interogare(int x, int y)
{
return (radacina(x) == radacina(y));
}
void reuniune(int x, int y)
{
int rx = radacina(x), ry = radacina(y);
if(nr[rx] < nr[ry]){
t[rx] = ry;
nr[ry] += nr[rx];
}
else{
t[ry] = rx;
nr[rx] += nr[ry];
}
}
int main()
{
int n, m;
in >> n >> m;
for(int i = 0; i < m; i++){
int code, x, y;
in >> code >> x >> y;
if(code == 1){
reuniune(x, y);
}
else{
if(interogare(x, y)){
out << "DA" << "\n";
}
else{
out << "NU" << "\n";
}
}
}
return 0;
}