This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ebi-fly13/Library
#include "tree/rooted_tree_hash.hpp"
根付き木のハッシュを計算する。木の同型判定に活用できる。
根を root として各部分木のハッシュを計算する。 $O(N)$
各深さに割り当てられたランダムな値を返す。
#pragma once #include <vector> #include "../graph/base.hpp" #include "../utility/hash.hpp" namespace ebi { template <int BASE_NUM = 2> struct rooted_tree_hash { private: using H = Hash<BASE_NUM>; static H get_basis(int d) { if (int(_basis.size()) <= d) _basis.emplace_back(H::get_basis()); return _basis[d]; } public: rooted_tree_hash() = default; template <class T> static std::vector<H> subtree_hash(const Graph<T> &g, int root = 0) { int n = g.size(); std::vector<H> hash(n, H::set(1)); std::vector<int> depth(n, 0); auto dfs = [&](auto &&self, int v, int par = -1) -> void { for (auto e : g[v]) { if (e.to == par) continue; self(self, e.to, v); depth[v] = std::max(depth[v], depth[e.to] + 1); } for (auto e : g[v]) { if (e.to == par) continue; hash[v] *= hash[e.to]; } if (hash[v] == H::set(1)) hash[v] = H::set(0); hash[v] += get_basis(depth[v]); return; }; dfs(dfs, root); return hash; } static std::vector<H> basis() { return _basis; } private: static std::vector<H> _basis; }; template <int BASE_NUM> std::vector<Hash<BASE_NUM>> rooted_tree_hash<BASE_NUM>::_basis = {}; } // namespace ebi
#line 2 "tree/rooted_tree_hash.hpp" #include <vector> #line 2 "graph/base.hpp" #include <cassert> #include <iostream> #include <ranges> #line 7 "graph/base.hpp" #line 2 "data_structure/simple_csr.hpp" #line 4 "data_structure/simple_csr.hpp" #include <utility> #line 6 "data_structure/simple_csr.hpp" namespace ebi { template <class E> struct simple_csr { simple_csr() = default; simple_csr(int n, const std::vector<std::pair<int, E>>& elements) : start(n + 1, 0), elist(elements.size()) { for (auto e : elements) { start[e.first + 1]++; } for (auto i : std::views::iota(0, n)) { start[i + 1] += start[i]; } auto counter = start; for (auto [i, e] : elements) { elist[counter[i]++] = e; } } simple_csr(const std::vector<std::vector<E>>& es) : start(es.size() + 1, 0) { int n = es.size(); for (auto i : std::views::iota(0, n)) { start[i + 1] = (int)es[i].size() + start[i]; } elist.resize(start.back()); for (auto i : std::views::iota(0, n)) { std::copy(es[i].begin(), es[i].end(), elist.begin() + start[i]); } } int size() const { return (int)start.size() - 1; } const auto operator[](int i) const { return std::ranges::subrange(elist.begin() + start[i], elist.begin() + start[i + 1]); } auto operator[](int i) { return std::ranges::subrange(elist.begin() + start[i], elist.begin() + start[i + 1]); } const auto operator()(int i, int l, int r) const { return std::ranges::subrange(elist.begin() + start[i] + l, elist.begin() + start[i + 1] + r); } auto operator()(int i, int l, int r) { return std::ranges::subrange(elist.begin() + start[i] + l, elist.begin() + start[i + 1] + r); } private: std::vector<int> start; std::vector<E> elist; }; } // namespace ebi #line 9 "graph/base.hpp" namespace ebi { template <class T> struct Edge { int from, to; T cost; int id; }; template <class E> struct Graph { using cost_type = E; using edge_type = Edge<cost_type>; Graph(int n_) : n(n_) {} Graph() = default; void add_edge(int u, int v, cost_type c) { buff.emplace_back(u, edge_type{u, v, c, m}); edges.emplace_back(edge_type{u, v, c, m++}); } void add_undirected_edge(int u, int v, cost_type c) { buff.emplace_back(u, edge_type{u, v, c, m}); buff.emplace_back(v, edge_type{v, u, c, m}); edges.emplace_back(edge_type{u, v, c, m}); m++; } void read_tree(int offset = 1, bool is_weighted = false) { read_graph(n - 1, offset, false, is_weighted); } void read_parents(int offset = 1) { for (auto i : std::views::iota(1, n)) { int p; std::cin >> p; p -= offset; add_undirected_edge(p, i, 1); } build(); } void read_graph(int e, int offset = 1, bool is_directed = false, bool is_weighted = false) { for (int i = 0; i < e; i++) { int u, v; std::cin >> u >> v; u -= offset; v -= offset; if (is_weighted) { cost_type c; std::cin >> c; if (is_directed) { add_edge(u, v, c); } else { add_undirected_edge(u, v, c); } } else { if (is_directed) { add_edge(u, v, 1); } else { add_undirected_edge(u, v, 1); } } } build(); } void build() { assert(!prepared); csr = simple_csr<edge_type>(n, buff); buff.clear(); prepared = true; } int size() const { return n; } int node_number() const { return n; } int edge_number() const { return m; } edge_type get_edge(int i) const { return edges[i]; } std::vector<edge_type> get_edges() const { return edges; } const auto operator[](int i) const { return csr[i]; } auto operator[](int i) { return csr[i]; } private: int n, m = 0; std::vector<std::pair<int,edge_type>> buff; std::vector<edge_type> edges; simple_csr<edge_type> csr; bool prepared = false; }; } // namespace ebi #line 2 "utility/hash.hpp" #include <array> #line 2 "modint/modint61.hpp" #line 4 "modint/modint61.hpp" #include <cstdint> #line 6 "modint/modint61.hpp" #line 2 "modint/base.hpp" #include <concepts> #line 6 "modint/base.hpp" namespace ebi { template <class T> concept Modint = requires(T a, T b) { a + b; a - b; a * b; a / b; a.inv(); a.val(); a.pow(std::declval<long long>()); T::mod(); }; template <Modint mint> std::istream &operator>>(std::istream &os, mint &a) { long long x; os >> x; a = x; return os; } template <Modint mint> std::ostream &operator<<(std::ostream &os, const mint &a) { return os << a.val(); } } // namespace ebi #line 8 "modint/modint61.hpp" namespace ebi { struct modint61 { private: using mint = modint61; using u64 = std::uint64_t; constexpr static u64 m = (1ull << 61) - 1; constexpr static u64 MASK31 = (1ull << 31) - 1; constexpr static u64 MASK30 = (1ull << 30) - 1; public: constexpr static u64 mod() { return m; } constexpr modint61() : _v(0) {} constexpr modint61(long long v) { v %= (long long)umod(); if (v < 0) v += (long long)umod(); _v = u64(v); } constexpr u64 val() const { return _v; } constexpr u64 value() const { return val(); } constexpr mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } constexpr mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } constexpr mint &operator+=(const mint &rhs) { _v += rhs._v; _v = safe_mod(_v); return *this; } constexpr mint &operator-=(const mint &rhs) { if (_v < rhs._v) _v += umod(); assert(_v >= rhs._v); _v -= rhs._v; return *this; } constexpr mint &operator*=(const mint &rhs) { u64 au = _v >> 31, ad = _v & MASK31; u64 bu = rhs._v >> 31, bd = rhs._v & MASK31; u64 mid = ad * bu + au * bd; u64 midu = mid >> 30; u64 midd = mid & MASK30; _v = (au * bu * 2 + midu + (midd << 31) + ad * bd); _v = safe_mod(_v); return *this; } constexpr mint &operator/=(const mint &rhs) { return *this *= rhs.inv(); } constexpr mint pow(long long n) const { assert(0 <= n); mint x = *this, res = 1; while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } constexpr mint inv() const { assert(_v); return pow(umod() - 2); } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs.val() == rhs.val(); } friend bool operator!=(const mint &lhs, const mint &rhs) { return !(lhs == rhs); } friend bool operator<(const mint &lhs, const mint &rhs) { return lhs._v < rhs._v; } friend bool operator>(const mint &lhs, const mint &rhs) { return rhs < lhs; } private: u64 _v = 0; constexpr static u64 umod() { return m; } constexpr u64 safe_mod(const u64 &a) { u64 au = a >> 61; u64 ad = a & umod(); u64 res = au + ad; if (res >= umod()) res -= umod(); return res; } }; } // namespace ebi #line 2 "utility/random_number_generator.hpp" #line 4 "utility/random_number_generator.hpp" #include <random> namespace ebi { struct random_number_generator { random_number_generator(int seed = -1) { if (seed < 0) seed = rnd(); mt.seed(seed); } void set_seed(int seed) { mt.seed(seed); } template <class T> T get(T a, T b) { std::uniform_int_distribution<T> dist(a, b - 1); return dist(mt); } private: std::mt19937_64 mt; std::random_device rnd; }; } // namespace ebi #line 7 "utility/hash.hpp" namespace ebi { template <int BASE_NUM = 2> struct Hash : std::array<modint61, BASE_NUM> { private: using std::array<modint61, BASE_NUM>::array; using std::array<modint61, BASE_NUM>::operator=; public: Hash() : std::array<modint61, BASE_NUM>() {} constexpr static Hash set(const modint61 &a) { Hash res; std::fill(res.begin(), res.end(), a); return res; } constexpr Hash &operator+=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] += rhs[i]; } return *this; } constexpr Hash &operator-=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] -= rhs[i]; } return *this; } constexpr Hash &operator*=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] *= rhs[i]; } return *this; } constexpr Hash &operator+=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] += rhs; } return *this; } constexpr Hash &operator-=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] -= rhs; } return *this; } constexpr Hash &operator*=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] *= rhs; } return *this; } Hash operator+(const Hash &rhs) const { return Hash(*this) += rhs; } Hash operator-(const Hash &rhs) const { return Hash(*this) -= rhs; } Hash operator*(const Hash &rhs) const { return Hash(*this) *= rhs; } Hash operator+(const modint61 &rhs) const { return Hash(*this) += rhs; } Hash operator-(const modint61 &rhs) const { return Hash(*this) -= rhs; } Hash operator*(const modint61 &rhs) const { return Hash(*this) *= rhs; } Hash pow(long long n) const { Hash a = *this, res = set(1); while (n) { if (n & 1) res *= a; a *= a; n >>= 1; } return res; } static Hash get_basis() { static random_number_generator rng; Hash h; for (int i = 0; i < BASE_NUM; i++) { h[i] = rng.get<std::uint64_t>(0, modint61::mod() - 1) + 1; } return h; } Hash inv() const { Hash h; for (int i = 0; i < BASE_NUM; i++) { h[i] = (*this)[i].inv(); } return h; } static Hash get_basis_primitive() { static random_number_generator rng; Hash h; for (int i = 0; i < BASE_NUM; i++) { while (!is_primitive( (h[i] = rng.get<std::uint64_t>(0, modint61::mod() - 1) + 1) .val())) ; } return h; } private: static bool is_primitive(long long x) { for (long long d : {2, 3, 5, 7, 11, 13, 31, 41, 61, 151, 331, 1321}) { if (modint61(x).pow((modint61::mod() - 1) / d).val() <= 1) return false; } return true; } }; } // namespace ebi #line 7 "tree/rooted_tree_hash.hpp" namespace ebi { template <int BASE_NUM = 2> struct rooted_tree_hash { private: using H = Hash<BASE_NUM>; static H get_basis(int d) { if (int(_basis.size()) <= d) _basis.emplace_back(H::get_basis()); return _basis[d]; } public: rooted_tree_hash() = default; template <class T> static std::vector<H> subtree_hash(const Graph<T> &g, int root = 0) { int n = g.size(); std::vector<H> hash(n, H::set(1)); std::vector<int> depth(n, 0); auto dfs = [&](auto &&self, int v, int par = -1) -> void { for (auto e : g[v]) { if (e.to == par) continue; self(self, e.to, v); depth[v] = std::max(depth[v], depth[e.to] + 1); } for (auto e : g[v]) { if (e.to == par) continue; hash[v] *= hash[e.to]; } if (hash[v] == H::set(1)) hash[v] = H::set(0); hash[v] += get_basis(depth[v]); return; }; dfs(dfs, root); return hash; } static std::vector<H> basis() { return _basis; } private: static std::vector<H> _basis; }; template <int BASE_NUM> std::vector<Hash<BASE_NUM>> rooted_tree_hash<BASE_NUM>::_basis = {}; } // namespace ebi