Pagini recente » Cod sursa (job #1480373) | Cod sursa (job #1545308) | Cod sursa (job #1165986) | Cod sursa (job #2218286) | Cod sursa (job #2861968)
#include <stdio.h>
#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];
}
//cout << root << " ";
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";
} else {
fout << "NU";
}
fout << "\n";
}
}
return 0;
}
/*
4 5
1 1 2
1 3 4
1 1 3
2 2 4
2 1 4
*/