Pagini recente » Cod sursa (job #1105504) | Cod sursa (job #2706937) | Cod sursa (job #2099516) | Cod sursa (job #2282206) | Cod sursa (job #2864054)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int MAX_SIZE = 100005;
int representants[MAX_SIZE];
int findRoot(int peak) {
int root = representants[peak];
while (root != representants[root]) {
root = representants[root];
}
while (representants[peak] != root) {
int aux = representants[peak];
representants[peak] = root;
peak = aux;
}
return root;
}
void unite(int start, int end) {
representants[start] = representants[end];
}
int main() {
int peaks, arches;
fin >> peaks >> arches;
for (int i = 1; i <= peaks; ++i) {
representants[i] = i;
}
for (int i = 1; i <= arches; ++i) {
int operation, start, end;
fin >> operation >> start >> end;
if (operation == 1) {
unite(findRoot(start), findRoot(end));
} else {
if (findRoot(start) == findRoot(end)) {
fout << "DA\n";
} else {
fout << "NU\n";
}
}
}
return 0;
}