QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#189421 | #6851. Cyclically Isomorphic | jrjyy | AC ✓ | 65ms | 3968kb | C++20 | 4.4kb | 2023-09-27 14:40:27 | 2023-09-27 14:40:27 |
Judging History
answer
#include <bits/stdc++.h>
#include <random>
using i64 = long long;
struct DSU {
std::vector<int> fa, sz;
DSU(int n = 0) { init(n); }
void init(int n) {
fa.resize(n), std::iota(fa.begin(), fa.end(), 0);
sz.assign(n, 1);
}
int find(int x) {
while (x != fa[x]) x = fa[x] = fa[fa[x]];
return x;
}
bool same(int x, int y) { return find(x) == find(y); }
bool merge(int x, int y) {
if ((x = find(x)) == (y = find(y))) return false;
return sz[x] += sz[y], fa[y] = x, true;
}
int size(int x) { return sz[find(x)]; }
};
template <typename T> constexpr T power(T a, i64 b) {
T c{1}; for (; b; b /= 2, a *= a) if (b & 1) c *= a;
return c;
}
template <int P> struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x_) : x{norm(x_ % getMod())} {}
static int Mod;
constexpr static int getMod() { return P > 0 ? P : Mod; }
constexpr static void setMod(int Mod_) { Mod = Mod_; }
constexpr int up(int x) const {
if (x < 0) x += getMod();
return x;
}
constexpr int down(int x) const {
if (x >= getMod()) x -= getMod();
return x;
}
constexpr int norm(int x) const {
return up(down(x));
}
constexpr int val() const { return x; }
explicit constexpr operator int() const { return x; }
constexpr MInt operator-() const {
MInt res; res.x = norm(getMod() - x); return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator+=(MInt rhs) & { return x = down(x + rhs.x), *this; }
constexpr MInt &operator-=(MInt rhs) & { return x = up(x - rhs.x), *this; }
constexpr MInt &operator*=(MInt rhs) & { return x = 1ll * x * rhs.x % getMod(), *this; }
constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); }
friend constexpr MInt operator+(MInt lhs, MInt rhs) { return lhs += rhs; }
friend constexpr MInt operator-(MInt lhs, MInt rhs) { return lhs -= rhs; }
friend constexpr MInt operator*(MInt lhs, MInt rhs) { return lhs *= rhs; }
friend constexpr MInt operator/(MInt lhs, MInt rhs) { return lhs /= rhs; }
friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
friend constexpr bool operator<(MInt lhs, MInt rhs) { return lhs.val() < rhs.val(); }
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 x = 0; is >> x, a = MInt(x); return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
};
template <int P> int MInt<P>::Mod = P;
template <> int MInt<0>::Mod = 1;
template <int V, int P> constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 1011451423;
using Z = MInt<P>;
void solve() {
std::mt19937 rnd(std::random_device{}());
const Z B1 = rnd() % P, B2 = rnd() % P;
int n, m;
std::cin >> n >> m;
std::vector<std::string> a(n);
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
std::pair<Z, Z> pw = {power(B1, m - 1), power(B2, m - 1)};
std::map<std::pair<Z, Z>, int> mp;
DSU dsu(n);
for (int i = 0; i < n; ++i) {
std::pair<Z, Z> h{};
for (int j = 0; j < m; ++j) {
h.first = h.first * B1 + (a[i][j] - 'a');
h.second = h.second * B2 + (a[i][j] - 'a');
}
for (int j = 0; j < m; ++j) {
if (mp.count(h)) {
dsu.merge(mp[h], i);
break;
}
h.first = (h.first - (a[i][j] - 'a') * pw.first) * B1 + (a[i][j] - 'a');
h.second = (h.second - (a[i][j] - 'a') * pw.second) * B2 + (a[i][j] - 'a');
}
if (dsu.find(i) == i) {
mp[h] = i;
}
}
int q;
std::cin >> q;
while (q--) {
int x, y;
std::cin >> x >> y;
--x, --y;
if (dsu.same(x, y)) {
std::cout << "Yes\n";
} else {
std::cout << "No\n";
}
}
}
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 65ms
memory: 3968kb
input:
5 10 10000 afcadffafeeebdfedfdafecfdbeecbcadfedcffaedecbacffedeccacfcbfbbdbcefbdefefebcbeacfbaafddbabcbfaeddaaefebbddafaaaaaedcbbdaafaffcbeeebfbbeedebabfbdbaceabfcccfedbdeafccbaffdeadbefbfeadbbedeeabbaafebaceeaeedbaddfecdefaabdbcfadadaebbdeedcfeeabbbdbcefdadbeecafcabbddddbcacadbbcddeddbbeecbdadefbaa...
output:
No No No Yes Yes No No No Yes No No No No No No No No No No No No No Yes No No No No No No No No No No Yes No No No Yes No No No No No No No No Yes No Yes No No No No No Yes No No No No No No No No No Yes No No Yes No No No No No No No No No No No Yes No Yes No Yes No No No No No No No No Yes Yes Ye...
result:
ok 500000 lines