variable namespace, function declaration fixes

This commit is contained in:
ProgramSnail 2023-04-29 12:33:05 +03:00
parent c31b20fa24
commit f973f65b5b
17 changed files with 511 additions and 98 deletions

View file

@ -40,9 +40,11 @@ private:
std::unordered_map<T, IdType> value_to_id_;
};
class Union { // TODO: recall right algorithm name
class GroupsManager { // TODO: recall right algorithm name
public:
Union(size_t n) {
GroupsManager() = default;
explicit GroupsManager(size_t n) {
edges_.resize(n);
ranks_.resize(n);
@ -52,26 +54,34 @@ public:
}
}
void Connect(size_t u, size_t v) { // TODO: recall choice algorithm
u = GetRoot(u);
v = GetRoot(v);
void Unite(size_t u, size_t v) { // TODO: recall choice algorithm
u = GetGroupRoot(u);
v = GetGroupRoot(v);
if (ranks_[v] >= ranks_[u]) {
edges_[u] = v;
ranks_[v] = std::max(ranks_[u] + 1, ranks_[v]);
} else {
edges_[v] = u;
// always ranks_[u] > ranks_[v]
}
}
bool IsConnected(size_t u, size_t v) {
return GetRoot(u) == GetRoot(v);
bool IsInOneGroup(size_t u, size_t v) {
return GetGroupRoot(u) == GetGroupRoot(v);
}
size_t GetRoot(size_t v) {
size_t AddElement() {
size_t id = edges_.size();
edges_.push_back(id);
ranks_.push_back(1);
return id;
}
size_t GetGroupRoot(size_t v) {
if (edges_[v] == v) {
return v;
}
return edges_[v] = GetRoot(edges_[v]);
return edges_[v] = GetGroupRoot(edges_[v]);
}
private:
std::vector<size_t> edges_;