CompetitiveProgrammingCpp

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

View the Project on GitHub

:heavy_check_mark: Test/DataStructure/DisjointSetUnion_Potential.test.cpp

Depends on

Code

#include <vector>

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

#include <iostream>

#include <ranges>


// begin:tag includes

#include "./../../Library/DataStructure/DisjointSetUnion.hpp"

#include "./../../Library/Math/ModInt.hpp"

// end:tag includes


constexpr long long MOD = 998244353;
using mint = mtd::ModInt<MOD>;

signed main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(0);

  int n, q;
  std::cin >> n >> q;

  auto dsu = mtd::PotentialDisjointSetUnion<mint>(n);

  for ([[maybe_unused]] auto _ : std::views::iota(0, q)) {
    int t;
    std::cin >> t;
    if (t == 0) {
      int u, v, x;
      std::cin >> u >> v >> x;

      if (!dsu.isSame(u, v) || dsu.diff(u, v) == x) {
        std::cout << 1 << std::endl;
        dsu.unite(u, v, x);
      } else {
        std::cout << 0 << std::endl;
      }
    } else {
      int u, v;
      std::cin >> u >> v;

      if (dsu.isSame(u, v)) {
        std::cout << dsu.diff(u, v) << std::endl;
      } else {
        std::cout << -1 << std::endl;
      }
    }
  }
}
#line 1 "Test/DataStructure/DisjointSetUnion_Potential.test.cpp"
#include <vector>

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

#include <iostream>

#include <ranges>


// begin:tag includes

#line 2 "Library/DataStructure/DisjointSetUnion.hpp"

#line 4 "Library/DataStructure/DisjointSetUnion.hpp"
#include <numeric>

#line 6 "Library/DataStructure/DisjointSetUnion.hpp"

namespace mtd {

  template <class T = int>
  class PotentialDisjointSetUnion {
    std::vector<int> m_root;
    std::vector<int> m_rank;
    std::vector<int> m_size;
    std::vector<T> m_potential;

  public:
    PotentialDisjointSetUnion() = delete;
    PotentialDisjointSetUnion(int n)
        : m_root(n), m_rank(n), m_size(n, 1), m_potential(n) {
      std::iota(m_root.begin(), m_root.end(), 0);
    }

    auto unite(int x, int y, T p = 0) {
      p += potential(x);
      p -= potential(y);
      x = root(x);
      y = root(y);
      if (x == y) { return false; }
      if (m_rank[x] < m_rank[y]) {
        std::swap(x, y);
        p = -p;
      }
      if (m_rank[x] == m_rank[y]) { ++m_rank[x]; }
      m_size[x] = m_size[y] = size(x) + size(y);
      m_root[y] = x;
      m_potential[y] = p;
      return true;
    }

    auto root(int x) -> int {
      if (m_root[x] == x) { return x; }
      int r = root(m_root[x]);
      m_potential[x] += m_potential[m_root[x]];
      return m_root[x] = r;
    }

    auto potential(int x) -> T {
      root(x);
      return m_potential[x];
    }

    auto size(int x) -> int {
      if (m_root[x] == x) { return m_size[x]; }
      return size(m_root[x] = root(m_root[x]));
    }

    auto isSame(int x, int y) { return root(x) == root(y); }

    auto diff(int x, int y) { return potential(y) - potential(x); }

    friend std::ostream& operator<<(std::ostream& os,
                                    const PotentialDisjointSetUnion& dsu) {
      for (const auto& x : dsu.m_root) { os << x << " "; }
      return os;
    }
  };
}  // namespace mtd

#line 2 "Library/Math/ModInt.hpp"

#line 4 "Library/Math/ModInt.hpp"
#include <iterator>

#line 2 "Library/Math/Math.hpp"

#include <cmath>

#line 5 "Library/Math/Math.hpp"
#include <optional>

#line 7 "Library/Math/Math.hpp"
#include <unordered_map>

#line 9 "Library/Math/Math.hpp"

#line 2 "Library/Math/EuclideanAlgorithm.hpp"

#line 5 "Library/Math/EuclideanAlgorithm.hpp"
#include <tuple>


namespace mtd {

  class EuclideanAlgorithm {
    using T = long long;

    // 大きすぎるとオーバーフローしてしまう

    const static inline T m_mx = 1e9;

    const T m_a;
    const T m_b;
    const T m_c;

    T m_gcd;
    T m_x;
    T m_y;

    auto excludedEuclidAlgorithm(T a, T b) -> std::tuple<T, T, T> {
      if (a < 0) {
        auto [g, x, y] = excludedEuclidAlgorithm(-a, -b);
        return {g, -x, -y};
      }
      if (b == 0) { return {a, 1, 0}; }
      auto [g, y, x] = excludedEuclidAlgorithm(b, a % b);
      y -= a / b * x;
      return {g, x, y};
    }

    auto kRange(T x, T b, T l) const -> std::pair<T, T> {
      // x + b * k >= l を満たす k の範囲を求める

      T xd = (l - x);
      if (b == 0 && x >= l) { return {-m_mx, m_mx}; }
      if (b == 0 && x < l) { return {m_mx, -m_mx}; }
      if (b > 0 && xd < 0) { return {xd / b, m_mx}; }
      if (b > 0 && xd >= 0) { return {(xd + b - 1) / b, m_mx}; }
      if (b < 0 && xd < 0) { return {-m_mx, (-xd) / (-b)}; }
      if (b < 0 && xd >= 0) { return {-m_mx, -(xd - b - 1) / (-b)}; }
      return {m_mx, -m_mx};
    }

  public:
    auto debug() const {
      std::cout << m_a << " * " << m_x << " + " << m_b << " * " << m_y << " = "
                << m_c << std::endl;
      std::cout << "calc: " << m_a * m_x + m_b * m_y << " = " << m_c
                << std::endl;
    }

    EuclideanAlgorithm(T a, T b, T c) : m_a(a), m_b(b), m_c(c) {
      if (a == 0 && b == 0) { throw std::runtime_error(""); }
      auto [g, x, y] = excludedEuclidAlgorithm(a, b);
      if (c % g > 0) {
        throw std::runtime_error(
            "There is no solution to the equation. c must be divisible by "
            "gcd(a,b).");
      }
      m_gcd = g;
      m_x = c / g * x;
      m_y = c / g * y;
    }
    EuclideanAlgorithm(T a, T b) : EuclideanAlgorithm(a, b, std::gcd(a, b)) {}

    auto gcd() const { return m_gcd; }
    auto get(T x, T y) const { return m_a * x + m_b * y; }
    auto get(T k) const -> std::pair<T, T> {
      if (m_b == 0) { return {m_x, m_y - k}; }
      if (m_a == 0) { return {m_x + k, m_y}; }
      return {m_x + m_b * k, m_y - m_a * k};
    }
    // x>=x_lとなるようなkの範囲

    auto getMinX(T x_l = 0) const -> std::pair<T, T> {
      return kRange(m_x, m_b, x_l);
    }
    // y>=y_lとなるようなkの範囲

    auto getMinY(T y_l = 0) const -> std::pair<T, T> {
      return kRange(m_y, -1 * m_a, y_l);
    }
    // x>=x_l, y>=y_lとなるようなkの範囲

    auto getMin(T x_l = 0, T y_l = 0) const -> std::pair<T, T> {
      auto [xl, xr] = getMinX(x_l);
      auto [yl, yr] = getMinY(y_l);
      return {std::max(xl, yl), std::min(xr, yr)};
    }
  };

}  // namespace mtd

#line 11 "Library/Math/Math.hpp"

namespace mtd {
  template <class T>
  class Math {
    const std::vector<T> m_fac;
    const std::vector<T> m_finv;

    auto constructFac(int s) {
      std::vector<T> fac(s);
      fac[0] = fac[1] = 1;
      for (long long i = 2; i < s; ++i) { fac[i] = fac[i - 1] * i; }
      return fac;
    }
    auto constructInv(int s) {
      std::vector<T> finv(s);
      finv[s - 1] = 1 / m_fac[s - 1];
      for (long long i = s - 2; i >= 0; --i) {
        finv[i] = finv[i + 1] * (i + 1);
      }
      return finv;
    }

  public:
    constexpr Math(int size = 3 * static_cast<int>(1e6))
        : m_fac(constructFac(size)), m_finv(constructInv(size)) {}

    /* O(log b) */
    static constexpr T pow(T a, long long b) {
      T ans = 1;
      while (b > 0) {
        if (b & 1) { ans *= a; }
        b >>= 1;
        a *= a;
      }
      return ans;
    }

    /* O(log mod) */
    template <class S>
    static constexpr std::optional<long long> log(S x, S y, S mod) {
      x %= mod;
      y %= mod;

      if (mod == 1) { return 0; }
      if (x == 0 && y == 0) { return 1; }
      if (x == 0 && y == 1) { return 0; }
      if (x == 0) { return std::nullopt; }
      if (y == 1) { return 0; }

      if (auto g = std::gcd(x, mod); g > 1) {
        if (y % g) { return std::nullopt; }
        auto nx = x / g;
        auto nmod = mod / g;
        auto ea = mtd::EuclideanAlgorithm(nx, -nmod, 1);
        auto [t, _] = ea.getMinX();
        auto [nx_inv, __] = ea.get(t);
        nx_inv %= nmod;
        if (auto ans = log(x, y / g * nx_inv, nmod); ans) {
          return ans.value() + 1;
        } else {
          return ans;
        }
      }

      auto s = static_cast<S>(std::sqrt(mod));
      S xe = y;
      std::unordered_map<S, S> map;
      map.reserve(s);
      for (auto i : std::views::iota(0, s)) {
        (xe *= x) %= mod;
        map[xe] = i + 1;
      }

      S xs = 1;
      for ([[maybe_unused]] auto _ : std::views::iota(0, s)) {
        (xs *= x) %= mod;
      }
      S xse = 1;
      for (auto i : std::views::iota(0, mod / s + 5)) {
        (xse *= xs) %= mod;
        if (map.contains(xse)) { return s * (i + 1) - map[xse]; }
      }
      return std::nullopt;
    }
    constexpr std::optional<long long> log(long long x,
                                           long long y) requires requires {
      typename T::value_type;
      T::mod();
    }
    { return log(x, y, T::mod()); }

    constexpr auto fact(int n) const { return (n < 0) ? 0 : m_fac[n]; }
    constexpr auto factInv(int n) const { return (n < 0 ? 0 : m_finv[n]); }
    constexpr auto comb(int n, int r) const {
      return fact(n) * factInv(r) * factInv(n - r);
    }
    constexpr auto perm(int n, int r) const { return fact(n) * factInv(n - r); }
  };
}  // namespace mtd

#line 7 "Library/Math/ModInt.hpp"

namespace mtd {

  template <int MOD, class T = long long>
  class ModInt {
  public:
    using value_type = T;
    T x;

    constexpr ModInt(T _x) : x(_x >= 0 ? _x % MOD : MOD + (_x % MOD)) {}
    constexpr ModInt() : ModInt(0) {}

    // 四則演算
    constexpr auto& operator+=(const ModInt<MOD, T>& m) {
      x += m.x;
      if (x >= MOD) { x -= MOD; }
      return *this;
    }
    constexpr auto& operator-=(const ModInt<MOD, T>& m) {
      x -= m.x;
      if (x < 0) { x += MOD; }
      return *this;
    }
    constexpr auto& operator*=(const ModInt<MOD, T>& m) {
      x *= m.x;
      if (x >= MOD) { x %= MOD; }
      return *this;
    }
    constexpr auto& operator/=(const ModInt<MOD, T>& m) {
      x *= mtd::Math<ModInt<MOD, T>>::pow(m.x, MOD - 2).x;
      if (x >= MOD) { x %= MOD; }
      return *this;
    }

    constexpr auto operator+(const ModInt<MOD, T>& m) const {
      auto t = *this;
      t += m;
      return t;
    }
    constexpr auto operator-(const ModInt<MOD, T>& m) const {
      auto t = *this;
      t -= m;
      return t;
    }
    constexpr auto operator*(const ModInt<MOD, T>& m) const {
      auto t = *this;
      t *= m;
      return t;
    }
    constexpr auto operator/(const ModInt<MOD, T>& m) const {
      auto t = *this;
      t /= m;
      return t;
    }

    constexpr auto& operator+=(const T& t) {
      return *this += ModInt<MOD, T>(t);
    }
    constexpr auto& operator-=(const T& t) {
      return *this -= ModInt<MOD, T>(t);
    }
    constexpr auto& operator*=(const T& n) {
      return *this *= ModInt<MOD, T>(n);
    }
    constexpr auto& operator/=(const T& n) {
      return *this /= ModInt<MOD, T>(n);
    }
    constexpr auto operator+(const T& t) const {
      return *this + ModInt<MOD, T>(t);
    }
    constexpr auto operator-(const T& t) const {
      return *this - ModInt<MOD, T>(t);
    }
    constexpr auto operator*(const T& t) const {
      return *this * ModInt<MOD, T>(t);
    }
    constexpr auto operator/(const T& t) const {
      return *this / ModInt<MOD, T>(t);
    }
    constexpr friend auto operator+(const T& t, const ModInt<MOD, T>& m) {
      return m + t;
    }
    constexpr friend auto operator-(const T& t, const ModInt<MOD, T>& m) {
      return -m + t;
    }
    constexpr friend auto operator*(const T& t, const ModInt<MOD, T>& m) {
      return m * t;
    }
    constexpr friend auto operator/(const T& t, const ModInt<MOD, T>& m) {
      return ModInt<MOD, T>(1) / m * t;
    }

    // 単項演算
    constexpr auto operator-() const { return ModInt<MOD, T>(0 - x); }

    // 比較演算
    constexpr auto operator!=(const ModInt<MOD, T>& m) const {
      return x != m.x;
    }
    constexpr auto operator==(const ModInt<MOD, T>& m) const {
      return !(x != m.x);
    }

    // 入出力
    constexpr friend std::ostream& operator<<(std::ostream& os,
                                              const ModInt<MOD, T>& m) {
      return os << m.x;
    }
    constexpr friend std::istream& operator>>(std::istream& is,
                                              ModInt<MOD, T>& m) {
      return is >> m.x;
    }

    constexpr auto val() const { return x; }
    static constexpr auto mod() { return MOD; }
  };

}  // namespace mtd
#line 10 "Test/DataStructure/DisjointSetUnion_Potential.test.cpp"
// end:tag includes


constexpr long long MOD = 998244353;
using mint = mtd::ModInt<MOD>;

signed main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(0);

  int n, q;
  std::cin >> n >> q;

  auto dsu = mtd::PotentialDisjointSetUnion<mint>(n);

  for ([[maybe_unused]] auto _ : std::views::iota(0, q)) {
    int t;
    std::cin >> t;
    if (t == 0) {
      int u, v, x;
      std::cin >> u >> v >> x;

      if (!dsu.isSame(u, v) || dsu.diff(u, v) == x) {
        std::cout << 1 << std::endl;
        dsu.unite(u, v, x);
      } else {
        std::cout << 0 << std::endl;
      }
    } else {
      int u, v;
      std::cin >> u >> v;

      if (dsu.isSame(u, v)) {
        std::cout << dsu.diff(u, v) << std::endl;
      } else {
        std::cout << -1 << std::endl;
      }
    }
  }
}
Back to top page