QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#153159#6851. Cyclically Isomorphicjrjyy#AC ✓59ms4048kbC++204.0kb2023-08-29 15:03:442023-08-29 15:03:45

Judging History

This is the latest submission verdict.

  • [2023-08-29 15:03:45]
  • Judged
  • Verdict: AC
  • Time: 59ms
  • Memory: 4048kb
  • [2023-08-29 15:03:44]
  • Submitted

answer

/* e.cpp */
#include <bits/stdc++.h>

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

constexpr Z B = 31;

void solve() {
    int n, m;
    std::cin >> n >> m;
    
    std::vector<std::string> a(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }

    Z x = power(B, m - 1);
    std::map<int, int> mp;
    DSU dsu(n);
    for (int i = 0; i < n; ++i) {
        Z h = 0;
        for (int j = 0; j < m; ++j) {
            h = h * B + (a[i][j] - 'a');
        }
        for (int j = 0; j < m; ++j) {
            if (mp.count(h.val())) {
                dsu.merge(mp[h.val()], i);
                break;
            }
            h = (h - (a[i][j] - 'a') * x) * B + (a[i][j] - 'a');
        }
        if (dsu.find(i) == i) {
            mp[h.val()] = 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: 59ms
memory: 4048kb

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