Nu exista pagina, dar poti sa o creezi ...
Cod sursa(job #2946564)
Utilizator | Data | 24 noiembrie 2022 23:35:21 | |
---|---|---|---|
Problema | Paduri de multimi disjuncte | Scor | 50 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.05 kb |
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
int parentVec[100005];
int maxN, opCount, opType, a, b;
ifstream myIn("disjoint.in");
ofstream myOut("disjoint.out");
int GetRoot(int a) {
if (parentVec[a] == 0) {
return a;
}
parentVec[a] = GetRoot(parentVec[a]);
return parentVec[a];
}
void UnionSet(int a, int b) {
int rootA = GetRoot(a);
int rootB = GetRoot(b);
if (a>b) {
parentVec[rootA] = rootB;
}
else
parentVec[rootB] = rootA;
}
int main() {
myIn >> maxN >> opCount;
for (int i = 0; i < opCount; i++) {
myIn >> opType >> a >> b;
if (opType == 1) {
// reuniune
UnionSet(a, b);
}
else {
// bool
if (GetRoot(a) == GetRoot(b)) {
myOut << "DA" << endl;
}
else {
myOut << "NU" << endl;
}
}
}
myIn.close();
myOut.close();
return 0;
}