rcpl

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

View the Project on GitHub ruthen71/rcpl

:heavy_check_mark: unionfind/test/unionfind_groups.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <iostream>

#include "../../unionfind/unionfind.hpp"

void test1_zero() {
    Unionfind uf(0);
    assert(uf.groups() == std::vector<std::vector<int>>());
}

void test2_empty() {
    Unionfind uf;
    assert(uf.groups() == std::vector<std::vector<int>>());
}

void test3_assign() {
    Unionfind uf;
    uf = Unionfind(10);
}

void test4_simple() {
    Unionfind uf(2);
    assert(uf.same(0, 1) == false);
    int x = uf.merge(0, 1);
    assert(uf.leader(0) == x);
    assert(uf.leader(1) == x);
    assert(uf.same(0, 1) == true);
    assert(uf.size(0) == 2);
}

void test5_line() {
    int n = 500000;
    Unionfind uf(n);
    for (int i = 0; i < n - 1; i++) {
        uf.merge(i, i + 1);
    }
    assert(uf.size(0) == n);
    assert(uf.groups().size() == 1);
}

void test6_line_reverse() {
    int n = 500000;
    Unionfind uf(n);
    for (int i = n - 2; i >= 0; i--) {
        uf.merge(i, i + 1);
    }
    assert(uf.size(0) == n);
    assert(uf.groups().size() == 1);
}

void test7_groups() {
    int n = 5;
    Unionfind uf(n);
    uf.merge(0, 1);
    uf.merge(2, 3);
    uf.merge(2, 4);
    auto gp = uf.groups();
    for (auto&& v : gp) std::sort(v.begin(), v.end());
    std::sort(gp.begin(), gp.end());
    std::vector<std::vector<int>> gp_sorted = {{0, 1}, {2, 3, 4}};
    assert(gp == gp_sorted);
}

int main() {
    test1_zero();
    test2_empty();
    test3_assign();
    test4_simple();
    test5_line();
    test6_line_reverse();
    test7_groups();
    int a, b;
    std::cin >> a >> b;
    std::cout << a + b << '\n';
    return 0;
}
#line 1 "unionfind/test/unionfind_groups.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <iostream>

#line 2 "unionfind/unionfind.hpp"

#include <algorithm>
#include <cassert>
#include <vector>

// Unionfind
struct Unionfind {
    int n;
    std::vector<int> parent;

    Unionfind() = default;

    explicit Unionfind(int n) : n(n), parent(n, -1) {}

    int leader(int x) {
        assert(0 <= x and x < n);
        if (parent[x] < 0) {
            return x;
        } else {
            return parent[x] = leader(parent[x]);
        }
    }

    int merge(int x, int y) {
        assert(0 <= x and x < n);
        assert(0 <= y and y < n);
        x = leader(x);
        y = leader(y);
        if (x == y) {
            return x;
        }
        if (-parent[x] < -parent[y]) {
            std::swap(x, y);
        }
        parent[x] += parent[y];
        parent[y] = x;
        return x;
    }

    bool same(int x, int y) {
        assert(0 <= x and x < n);
        assert(0 <= y and y < n);
        return leader(x) == leader(y);
    }

    int size(int x) {
        assert(0 <= x and x < n);
        return -parent[leader(x)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(n), group_size(n);
        for (int i = 0; i < n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(n);
        for (int i = 0; i < n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(std::remove_if(
                         result.begin(), result.end(),
                         [&](const std::vector<int>& v) { return v.empty(); }),
                     result.end());
        return result;
    }
};
#line 6 "unionfind/test/unionfind_groups.test.cpp"

void test1_zero() {
    Unionfind uf(0);
    assert(uf.groups() == std::vector<std::vector<int>>());
}

void test2_empty() {
    Unionfind uf;
    assert(uf.groups() == std::vector<std::vector<int>>());
}

void test3_assign() {
    Unionfind uf;
    uf = Unionfind(10);
}

void test4_simple() {
    Unionfind uf(2);
    assert(uf.same(0, 1) == false);
    int x = uf.merge(0, 1);
    assert(uf.leader(0) == x);
    assert(uf.leader(1) == x);
    assert(uf.same(0, 1) == true);
    assert(uf.size(0) == 2);
}

void test5_line() {
    int n = 500000;
    Unionfind uf(n);
    for (int i = 0; i < n - 1; i++) {
        uf.merge(i, i + 1);
    }
    assert(uf.size(0) == n);
    assert(uf.groups().size() == 1);
}

void test6_line_reverse() {
    int n = 500000;
    Unionfind uf(n);
    for (int i = n - 2; i >= 0; i--) {
        uf.merge(i, i + 1);
    }
    assert(uf.size(0) == n);
    assert(uf.groups().size() == 1);
}

void test7_groups() {
    int n = 5;
    Unionfind uf(n);
    uf.merge(0, 1);
    uf.merge(2, 3);
    uf.merge(2, 4);
    auto gp = uf.groups();
    for (auto&& v : gp) std::sort(v.begin(), v.end());
    std::sort(gp.begin(), gp.end());
    std::vector<std::vector<int>> gp_sorted = {{0, 1}, {2, 3, 4}};
    assert(gp == gp_sorted);
}

int main() {
    test1_zero();
    test2_empty();
    test3_assign();
    test4_simple();
    test5_line();
    test6_line_reverse();
    test7_groups();
    int a, b;
    std::cin >> a >> b;
    std::cout << a + b << '\n';
    return 0;
}
Back to top page