Pagini recente » Cod sursa (job #2261322) | Cod sursa (job #1159753) | Cod sursa (job #2535961) | Cod sursa (job #1487689) | Cod sursa (job #2942895)
#include <bits/stdc++.h>
using namespace std;
class UnionFind{
int NrNodes;
vector<int> Fathers;
public:
// Exclusiv pentru problema
void Solve();
// Functii union find
int Find(int);
void Union(int,int);
};
int UnionFind::Find(int Node) {
if(Fathers[Node] == 0)
return Node;
Fathers[Node] = Find(Fathers[Node]);
return Fathers[Node];
}
void UnionFind::Union(int NodeX, int NodeY) {
Fathers[NodeX] = NodeY;
}
void UnionFind::Solve() {
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin >> NrNodes;
Fathers = vector<int>(NrNodes + 1, 0);
int M;
fin >> M;
for(int i = 0; i < M; i++){
int cod, X, Y;
fin >> cod >> X >> Y;
int fatherX = Find(X), fatherY = Find(Y);
if(cod == 1){
Union(fatherX,fatherY);
} else{
if( fatherX== fatherY)
fout << "DA" << endl;
else
fout << "NU" << endl;
}
}
fin.close();
fout.close();
}
int main() {
UnionFind multime;
multime.Solve();
return 0;
}