Pagini recente » Cod sursa (job #2988626) | Cod sursa (job #665138) | Cod sursa (job #280390) | Cod sursa (job #1271810) | Cod sursa (job #1823102)
#include <iostream>
using namespace std;
class Nod{
public:
Nod* parinte;
int rang;
Nod();
Nod* find();
void unite(Nod* nod);
};
Nod::Nod(){
this->parinte = this;
this->rang = 0;
}
Nod* Nod::find(){
if(this != parinte){
this->parinte = parinte->find();
}
return this->parinte;
}
void Nod::unite(Nod* nod){
Nod *parinte1 = this->find(),
*parinte2 = nod->find();
if(parinte1 -> rang > parinte2 -> rang){
parinte2->parinte = parinte1;
}
else if(parinte1->rang < parinte2->rang){
parinte1->parinte = parinte2;
}
else{
parinte1->parinte = parinte2;
parinte2->rang++;
}
}
int main()
{
int n, m;
cin >> n >> m;
Nod** noduri = new Nod*[n+1];
for(int i = 1; i <= n; ++i){
noduri[i] = new Nod();
}
for(int i = 0; i < m; ++i){
int c, x, y;
cin >> c >> x >> y;
if(c == 1){
noduri[x]->unite(noduri[y]);
}
else{
if(noduri[x]->find()==noduri[y]->find()){
cout << "DA";
}
else{
cout << "NU";
}
}
}
for(int i = 0; i < n; ++i){
delete noduri[i];
}
return 0;
}