Pagini recente » Cod sursa (job #2421808) | Cod sursa (job #1728723) | Cod sursa (job #1415169) | Cod sursa (job #752347) | Cod sursa (job #2967410)
// Algoritm disjoint, uniune dupa rang
// Complexitate : O(n log* n)
#include <vector>
#include <bitset>
#include <fstream>
using namespace std;
const int N = 1e5 + 1;
int t[N], rang[N];
void Union(int x, int y) {
if (rang[x] > rang[y]){
t[y] = x;
} else {
t[x] = y;
if (rang[x] == rang[y]) rang[y]++;
}
}
int Find(int x) {
if (t[x] == 0)
return x;
int y = Find(t[x]);
t[x] = y;
return y;
}
int main() {
int n, queries;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
fin >> n >> queries;
for(int i = 1; i <= queries; i++) {
int op, x, y;
fin >> op >> x >> y;
x = Find(x);
y = Find(y);
if (op == 1)
Union(x, y);
else (x == y) ? fout << "DA\n" : fout << "NU\n";
}
fin.close();
fout.close();
}