competitive_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub knshnb/competitive_library

:heavy_check_mark: src/DataStructure/DynamicUnionFind.hpp

Verified with

Code

struct DynamicUnionFind {
    int cnt = 0;
    std::unordered_map<int, int> number;

    int root(int x) {
        if (!number.count(x)) number[x] = -1, cnt++;
        return number[x] < 0 ? x : number[x] = root(number[x]);
    }
    void unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return;
        if (number[y] < number[x]) std::swap(x, y);
        // yをxにマージ
        number[x] += number[y];
        number[y] = x;
        cnt--;
    }
    bool is_same(int x, int y) { return root(x) == root(y); }
    int size(int x) { return -number[root(x)]; }
};
#line 1 "src/DataStructure/DynamicUnionFind.hpp"
struct DynamicUnionFind {
    int cnt = 0;
    std::unordered_map<int, int> number;

    int root(int x) {
        if (!number.count(x)) number[x] = -1, cnt++;
        return number[x] < 0 ? x : number[x] = root(number[x]);
    }
    void unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return;
        if (number[y] < number[x]) std::swap(x, y);
        // yをxにマージ
        number[x] += number[y];
        number[y] = x;
        cnt--;
    }
    bool is_same(int x, int y) { return root(x) == root(y); }
    int size(int x) { return -number[root(x)]; }
};
Back to top page