Пример #1
0
    void unionSets(const T& a, const T& b) {
        DisjointSet<T>* sa = this->findSet(a);
        DisjointSet<T>* sb = this->findSet(b);

        if (sa->getAncestor() == sb->getAncestor()) {
            cerr << a << " and " << b << " are already in the same set!" << endl;
            return;
        }

        if (sa->getRank() > sb->getRank()) {
            sb->setParent(sa);
            sa->incRank(sb->getRank());
        } else {
            sa->setParent(sb);
            sb->incRank(sa->getRank());
        }
    }
Пример #2
0
    bool intersects(const T& a, const T& b) {
        DisjointSet<T>* sa = this->findSet(a);
        DisjointSet<T>* sb = this->findSet(b);

        return sa->getAncestor() == sb->getAncestor();
    }