CMakeLists.txt
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Dsu {
|
||||
public:
|
||||
struct Node {
|
||||
size_t parent, size;
|
||||
};
|
||||
|
||||
vector<Node> nodes;
|
||||
|
||||
explicit Dsu(const size_t size) : nodes(size) {
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
nodes[i] = {.parent = i, .size = 0};
|
||||
}
|
||||
}
|
||||
|
||||
size_t find(const size_t x) {
|
||||
if (nodes[x].parent == x) { return x; }
|
||||
return nodes[x].parent = find(nodes[x].parent);
|
||||
}
|
||||
|
||||
void unite(size_t x, size_t y) {
|
||||
if ((x = find(x)) == (y = find(y))) { return; }
|
||||
if (nodes[x].size > nodes[y].size) { swap(x, y); }
|
||||
nodes[x].parent = y;
|
||||
nodes[y].size += nodes[x].size;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int elements_count, operations_count;
|
||||
cin >> elements_count >> operations_count;
|
||||
Dsu dsu(elements_count);
|
||||
for (int _ = 0; _ < operations_count; _++) {
|
||||
int op, x, y;
|
||||
cin >> op >> x >> y;
|
||||
if (op == 1) { dsu.unite(x, y); }
|
||||
if (op == 2) { cout << (dsu.find(x) == dsu.find(y) ? 'Y' : 'N') << endl; }
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user