This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub hitonanode/cplib-cpp
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1613" #include "../tree_isomorphism.hpp" #include "../../modint.hpp" #include "../../unionfind/grid_unionfind.hpp" #include <algorithm> #include <array> #include <iostream> #include <string> #include <utility> #include <vector> using namespace std; using mint = ModInt<998244353>; pair<mint, mint> tree_hash(vector<string> S) { for (auto &s : S) s = "." + s + "."; const int W = S[0].size(); S.insert(S.begin(), string(W, '.')); S.push_back(string(W, '.')); const int H = S.size(); GridUnionFind uf(H, W); vector<pair<int, int>> dx4{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; vector<pair<int, int>> dx8{ {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; auto is_inner = [&](int i, int j) { return i >= 0 and i < H and j >= 0 and j < W; }; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { vector<pair<int, int>> *ptr = S[i][j] == '#' ? &dx8 : &dx4; for (auto p : *ptr) { int nx = i + p.first, ny = j + p.second; if (is_inner(nx, ny) and S[i][j] == S[nx][ny]) uf.unite(i, j, nx, ny); } } } std::vector<int> roots; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) roots.push_back(uf.find(i, j)); } sort(roots.begin(), roots.end()); roots.erase(unique(roots.begin(), roots.end()), roots.end()); tree_isomorphism<mint> iso(roots.size()); vector<int> seen(H * W); seen[uf.find(0, 0)] = 1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int r = uf.find(i, j); if (seen[r]) continue; seen[r] = 1; int outer = -1; int cur = i; while (cur >= 0) { if (uf.find(cur, j) == r) { outer = -1; } else if (outer < 0) { outer = uf.find(cur, j); } cur--; } int u = lower_bound(roots.begin(), roots.end(), r) - roots.begin(); int v = lower_bound(roots.begin(), roots.end(), outer) - roots.begin(); iso.add_edge(u, v); } } int rid = lower_bound(roots.begin(), roots.end(), uf.find(0, 0)) - roots.begin(); iso.build_hash(0, 141598, 181263479); return iso.hash[rid]; } int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int H, W; while (true) { cin >> H >> W; if (!H) break; vector<string> S(H); for (auto &s : S) cin >> s; int H2, W2; cin >> H2 >> W2; vector<string> T(H2); for (auto &s : T) cin >> s; cout << (tree_hash(S) == tree_hash(T) ? "yes" : "no") << '\n'; } }
#line 1 "tree/test/tree_isomorphism.aoj1613.test.cpp" #define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1613" #line 2 "tree/tree_isomorphism.hpp" #include <chrono> #include <utility> #include <vector> // Tree isomorphism with hashing (ハッシュによる木の同型判定) // Dependence: ModInt or ModIntRuntime // Reference: https://snuke.hatenablog.com/entry/2017/02/03/054210 // Verified: https://atcoder.jp/contests/nikkei2019-2-final/submissions/9044698 (ModInt) // https://atcoder.jp/contests/nikkei2019-2-final/submissions/9044745 (ModIntRuntime) template <typename ModInt> struct tree_isomorphism { using DoubleHash = std::pair<ModInt, ModInt>; using Edges = std::vector<std::vector<int>>; // vector<set<int>>; int V; Edges e; tree_isomorphism(int v) : V(v), e(v) {} void add_edge(int u, int v) { e[u].emplace_back(v); e[v].emplace_back(u); } static uint64_t splitmix64(uint64_t x) { // https://codeforces.com/blog/entry/62393 http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } DoubleHash get_hash(DoubleHash x) const { static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count(); return {splitmix64(x.first.val() + FIXED_RANDOM), splitmix64(x.second.val() + FIXED_RANDOM)}; } static void add_hash(DoubleHash &l, const DoubleHash &r) { l.first += r.first, l.second += r.second; } static DoubleHash subtract_hash(const DoubleHash &l, const DoubleHash &r) { return {l.first - r.first, l.second - r.second}; } std::vector<DoubleHash> hash; // hash of the tree, each node regarded as root std::vector<DoubleHash> hash_subtree; // hash of the subtree std::vector<DoubleHash> hash_par; // hash of the subtree whose root is parent[i], not containing i DoubleHash hash_p; // \in [1, hmod), should be set randomly DoubleHash hash_dfs1_(int now, int prv) { hash_subtree[now] = hash_p; for (auto nxt : e[now]) { if (nxt != prv) add_hash(hash_subtree[now], hash_dfs1_(nxt, now)); } return get_hash(hash_subtree[now]); } void hash_dfs2_(int now, int prv) { add_hash(hash[now], hash_subtree[now]); if (prv >= 0) hash_par[now] = subtract_hash(hash[prv], get_hash(hash_subtree[now])); for (auto nxt : e[now]) if (nxt != prv) { DoubleHash tmp = subtract_hash(hash[now], get_hash(hash_subtree[nxt])); add_hash(hash[nxt], get_hash(tmp)); hash_dfs2_(nxt, now); } } void build_hash(int root, int p1, int p2) { hash_p = std::make_pair(p1, p2); hash.resize(V), hash_subtree.resize(V), hash_par.resize(V); hash_dfs1_(root, -1); hash_dfs2_(root, -1); } }; #line 2 "modint.hpp" #include <cassert> #include <iostream> #include <set> #line 6 "modint.hpp" template <int md> struct ModInt { using lint = long long; constexpr static int mod() { return md; } static int get_primitive_root() { static int primitive_root = 0; if (!primitive_root) { primitive_root = [&]() { std::set<int> fac; int v = md - 1; for (lint i = 2; i * i <= v; i++) while (v % i == 0) fac.insert(i), v /= i; if (v > 1) fac.insert(v); for (int g = 1; g < md; g++) { bool ok = true; for (auto i : fac) if (ModInt(g).pow((md - 1) / i) == 1) { ok = false; break; } if (ok) return g; } return -1; }(); } return primitive_root; } int val_; int val() const noexcept { return val_; } constexpr ModInt() : val_(0) {} constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; } constexpr ModInt(lint v) { _setval(v % md + md); } constexpr explicit operator bool() const { return val_ != 0; } constexpr ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val_ + x.val_); } constexpr ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val_ - x.val_ + md); } constexpr ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val_ * x.val_ % md); } constexpr ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val_ * x.inv().val() % md); } constexpr ModInt operator-() const { return ModInt()._setval(md - val_); } constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; } constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; } constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; } constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; } friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; } friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; } friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; } friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; } constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; } constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; } constexpr bool operator<(const ModInt &x) const { return val_ < x.val_; } // To use std::map<ModInt, T> friend std::istream &operator>>(std::istream &is, ModInt &x) { lint t; return is >> t, x = ModInt(t), is; } constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { return os << x.val_; } constexpr ModInt pow(lint n) const { ModInt ans = 1, tmp = *this; while (n) { if (n & 1) ans *= tmp; tmp *= tmp, n >>= 1; } return ans; } static constexpr int cache_limit = std::min(md, 1 << 21); static std::vector<ModInt> facs, facinvs, invs; constexpr static void _precalculation(int N) { const int l0 = facs.size(); if (N > md) N = md; if (N <= l0) return; facs.resize(N), facinvs.resize(N), invs.resize(N); for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i; facinvs[N - 1] = facs.back().pow(md - 2); for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1); for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1]; } constexpr ModInt inv() const { if (this->val_ < cache_limit) { if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0}; while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return invs[this->val_]; } else { return this->pow(md - 2); } } constexpr ModInt fac() const { while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return facs[this->val_]; } constexpr ModInt facinv() const { while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return facinvs[this->val_]; } constexpr ModInt doublefac() const { lint k = (this->val_ + 1) / 2; return (this->val_ & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac()) : ModInt(k).fac() * ModInt(2).pow(k); } constexpr ModInt nCr(int r) const { if (r < 0 or this->val_ < r) return ModInt(0); return this->fac() * (*this - r).facinv() * ModInt(r).facinv(); } constexpr ModInt nPr(int r) const { if (r < 0 or this->val_ < r) return ModInt(0); return this->fac() * (*this - r).facinv(); } static ModInt binom(int n, int r) { static long long bruteforce_times = 0; if (r < 0 or n < r) return ModInt(0); if (n <= bruteforce_times or n < (int)facs.size()) return ModInt(n).nCr(r); r = std::min(r, n - r); ModInt ret = ModInt(r).facinv(); for (int i = 0; i < r; ++i) ret *= n - i; bruteforce_times += r; return ret; } // Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!) // Complexity: O(sum(ks)) template <class Vec> static ModInt multinomial(const Vec &ks) { ModInt ret{1}; int sum = 0; for (int k : ks) { assert(k >= 0); ret *= ModInt(k).facinv(), sum += k; } return ret * ModInt(sum).fac(); } // Catalan number, C_n = binom(2n, n) / (n + 1) // C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ... // https://oeis.org/A000108 // Complexity: O(n) static ModInt catalan(int n) { if (n < 0) return ModInt(0); return ModInt(n * 2).fac() * ModInt(n + 1).facinv() * ModInt(n).facinv(); } ModInt sqrt() const { if (val_ == 0) return 0; if (md == 2) return val_; if (pow((md - 1) / 2) != 1) return 0; ModInt b = 1; while (b.pow((md - 1) / 2) == 1) b += 1; int e = 0, m = md - 1; while (m % 2 == 0) m >>= 1, e++; ModInt x = pow((m - 1) / 2), y = (*this) * x * x; x *= (*this); ModInt z = b.pow(m); while (y != 1) { int j = 0; ModInt t = y; while (t != 1) j++, t *= t; z = z.pow(1LL << (e - j - 1)); x *= z, z *= z, y *= z; e = j; } return ModInt(std::min(x.val_, md - x.val_)); } }; template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1}; template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1}; template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0}; using ModInt998244353 = ModInt<998244353>; // using mint = ModInt<998244353>; // using mint = ModInt<1000000007>; #line 3 "unionfind/grid_unionfind.hpp" #include <iomanip> #include <numeric> #line 7 "unionfind/grid_unionfind.hpp" // CUT begin struct GridUnionFind { int H, W; std::vector<int> par, cou; using P = std::pair<int, int>; GridUnionFind(int H_, int W_) : H(H_), W(W_), par(H * W), cou(H * W, 1) { std::iota(par.begin(), par.end(), 0); } inline int id_(int h, int w) { return h * W + w; } inline bool coordinate_valid(int h, int w) { return h >= 0 and h < H and w >= 0 and w < W; } int _find(int x) { return (par[x] == x) ? x : (par[x] = _find(par[x])); } bool _unite(int x, int y) { x = _find(x), y = _find(y); if (x == y) return false; if (cou[x] < cou[y]) std::swap(x, y); par[y] = x, cou[x] += cou[y]; return true; } int find(int h, int w) { assert(coordinate_valid(h, w)); return _find(id_(h, w)); } bool unite(int h, int w, int h2, int w2) { assert(coordinate_valid(h, w) and coordinate_valid(h2, w2)); return _unite(id_(h, w), id_(h2, w2)); } int count(int h, int w) { return cou[find(h, w)]; } bool same(int h, int w, int h2, int w2) { return find(h, w) == find(h2, w2); } int find(P p) { return find(p.first, p.second); } bool unite(P p, P p2) { return unite(p.first, p.second, p2.first, p2.second); } int count(P p) { return count(p.first, p.second); } bool same(P p, P p2) { return same(p.first, p.second, p2.first, p2.second); } void merge_outer() { for (int h = 0; h < H - 1; h++) unite(h, 0, h + 1, 0), unite(h, W - 1, h + 1, W - 1); for (int w = 0; w < W - 1; w++) unite(0, w, 0, w + 1), unite(H - 1, w, H - 1, w + 1); } template <typename OStream> friend OStream &operator<<(OStream &os, GridUnionFind &g) { constexpr int WW = 3; os << "[(" << g.H << " * " << g.W << " grid)\n"; for (int i = 0; i < g.H; i++) { for (int j = 0; j < g.W - 1; j++) { os << std::setw(WW) << g.find(i, j) << (g.same(i, j, i, j + 1) ? '-' : ' '); } os << std::setw(WW) << g.find(i, g.W - 1) << '\n'; if (i < g.H - 1) { for (int j = 0; j < g.W; j++) os << std::setw(WW + 1) << (g.same(i, j, i + 1, j) ? "| " : " "); os << "\n"; } } os << "]\n"; return os; } }; #line 5 "tree/test/tree_isomorphism.aoj1613.test.cpp" #include <algorithm> #include <array> #line 8 "tree/test/tree_isomorphism.aoj1613.test.cpp" #include <string> #line 11 "tree/test/tree_isomorphism.aoj1613.test.cpp" using namespace std; using mint = ModInt<998244353>; pair<mint, mint> tree_hash(vector<string> S) { for (auto &s : S) s = "." + s + "."; const int W = S[0].size(); S.insert(S.begin(), string(W, '.')); S.push_back(string(W, '.')); const int H = S.size(); GridUnionFind uf(H, W); vector<pair<int, int>> dx4{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; vector<pair<int, int>> dx8{ {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; auto is_inner = [&](int i, int j) { return i >= 0 and i < H and j >= 0 and j < W; }; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { vector<pair<int, int>> *ptr = S[i][j] == '#' ? &dx8 : &dx4; for (auto p : *ptr) { int nx = i + p.first, ny = j + p.second; if (is_inner(nx, ny) and S[i][j] == S[nx][ny]) uf.unite(i, j, nx, ny); } } } std::vector<int> roots; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) roots.push_back(uf.find(i, j)); } sort(roots.begin(), roots.end()); roots.erase(unique(roots.begin(), roots.end()), roots.end()); tree_isomorphism<mint> iso(roots.size()); vector<int> seen(H * W); seen[uf.find(0, 0)] = 1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int r = uf.find(i, j); if (seen[r]) continue; seen[r] = 1; int outer = -1; int cur = i; while (cur >= 0) { if (uf.find(cur, j) == r) { outer = -1; } else if (outer < 0) { outer = uf.find(cur, j); } cur--; } int u = lower_bound(roots.begin(), roots.end(), r) - roots.begin(); int v = lower_bound(roots.begin(), roots.end(), outer) - roots.begin(); iso.add_edge(u, v); } } int rid = lower_bound(roots.begin(), roots.end(), uf.find(0, 0)) - roots.begin(); iso.build_hash(0, 141598, 181263479); return iso.hash[rid]; } int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int H, W; while (true) { cin >> H >> W; if (!H) break; vector<string> S(H); for (auto &s : S) cin >> s; int H2, W2; cin >> H2 >> W2; vector<string> T(H2); for (auto &s : T) cin >> s; cout << (tree_hash(S) == tree_hash(T) ? "yes" : "no") << '\n'; } }