proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: tests/random_tree.test.cpp

Depends on

Code

// competitive-verifier: STANDALONE

#include <iostream>
#include <random>
#include "atcoder/dsu.hpp"
#include "tools/assert_that.hpp"
#include "tools/random_tree.hpp"

using ll = long long;

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  std::random_device seed_gen;
  std::mt19937 engine(seed_gen());

  for (ll n = 1; n <= 1000; ++n) {
    tools::random_tree<ll> dist(n);
    for (ll i = 0; i < 10; ++i) {
      atcoder::dsu dsu(n);
      ll edge_count = 0;
      for (const auto& [u, v] : dist(engine)) {
        dsu.merge(u, v);
        ++edge_count;
      }
      assert_that(dsu.groups().size() == 1);
      assert_that(edge_count == n - 1);
    }
  }

  return 0;
}
#line 1 "tests/random_tree.test.cpp"
// competitive-verifier: STANDALONE

#include <iostream>
#include <random>
#line 1 "lib/ac-library/atcoder/dsu.hpp"



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

namespace atcoder {

// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
  public:
    dsu() : _n(0) {}
    explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    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;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

}  // namespace atcoder


#line 1 "tools/assert_that.hpp"



#line 5 "tools/assert_that.hpp"
#include <cstdlib>

#define assert_that_impl(cond, file, line, func) do {\
  if (!cond) {\
    ::std::cerr << file << ':' << line << ": " << func << ": Assertion `" << #cond << "' failed." << '\n';\
    ::std::exit(EXIT_FAILURE);\
  }\
} while (false)
#define assert_that(...) assert_that_impl((__VA_ARGS__), __FILE__, __LINE__, __func__)


#line 1 "tools/random_tree.hpp"



#include <cstddef>
#line 7 "tools/random_tree.hpp"
#include <utility>
#include <numeric>
#line 10 "tools/random_tree.hpp"

// Source: https://twitter.com/anta_prg/status/869633557362163712
// License: unknown
// Author: anta

namespace tools {

  template <typename T>
  class random_tree {
  private:
    ::std::size_t m_size;

  public:
    random_tree() = default;
    random_tree(const ::tools::random_tree<T>&) = default;
    random_tree(::tools::random_tree<T>&&) = default;
    ~random_tree() = default;
    ::tools::random_tree<T>& operator=(const ::tools::random_tree<T>&) = default;
    ::tools::random_tree<T>& operator=(::tools::random_tree<T>&&) = default;

    explicit random_tree(const ::std::size_t n) : m_size(n) {
      assert(n >= 1);
    }

    ::std::size_t size() const {
      return this->m_size;
    }

    template <typename R>
    ::std::vector<::std::pair<T, T>> operator()(R& engine) const {
      ::std::vector<::std::pair<T, T>> edges;

      ::std::vector<T> perm(this->size());
      ::std::iota(perm.begin(), perm.end(), 0);
      for (::std::size_t i = 0; i + 1 < this->size(); ++i) {
        const auto x = ::std::uniform_int_distribution<::std::size_t>(0, this->size() - i - 2)(engine);
        const auto y = ::std::uniform_int_distribution<::std::size_t>(0, this->size() - 1)(engine);
        ::std::swap(perm[i + 1], perm[i + 1 + x]);
        if (y < i + 1) ::std::swap(perm[i], perm[y]);
        edges.emplace_back(perm[i], perm[i + 1]);
      }

      return edges;
    }
  };
}


#line 8 "tests/random_tree.test.cpp"

using ll = long long;

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  std::random_device seed_gen;
  std::mt19937 engine(seed_gen());

  for (ll n = 1; n <= 1000; ++n) {
    tools::random_tree<ll> dist(n);
    for (ll i = 0; i < 10; ++i) {
      atcoder::dsu dsu(n);
      ll edge_count = 0;
      for (const auto& [u, v] : dist(engine)) {
        dsu.merge(u, v);
        ++edge_count;
      }
      assert_that(dsu.groups().size() == 1);
      assert_that(edge_count == n - 1);
    }
  }

  return 0;
}
Back to top page