結果

問題 No.2291 Union Find Estimate
ユーザー 👑 NachiaNachia
提出日時 2023-05-05 23:09:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 2,646 bytes
コンパイル時間 762 ms
コンパイル使用メモリ 83,568 KB
実行使用メモリ 5,052 KB
最終ジャッジ日時 2023-08-15 06:10:53
合計ジャッジ時間 1,797 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 36 ms
4,376 KB
testcase_03 AC 5 ms
5,052 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <algorithm>

namespace nachia {

struct Dsu{
private:
    int N;
    std::vector<int> P;
    std::vector<int> H;
public:
    Dsu() : N(0) {}
    Dsu(int n) : N(n), P(n, -1), H(n) {
        for(int i=0; i<n; i++) H[i] = i;
    }
    int leader(int u){
        if(P[u] < 0) return u;
        int v = P[u];
        while(P[v] >= 0){ P[u] = P[v]; u = v; v = P[v]; }
        return P[u];
    }
    int append(){
        int n = P.size();
        P.push_back(-1);
        H.push_back(n);
        return n;
    }
    int label(int u){ return H[leader(u)]; }
    int operator[](int u){ return H[leader(u)]; }
    void merge(int u, int v, int newLabel){
        if(newLabel < 0) newLabel = u;
        u = leader(u);
        v = leader(v);
        if(u == v){ H[u] = newLabel; return; }
        N--;
        if(-P[u] < -P[v]) std::swap(u, v);
        P[u] += P[v];
        H[P[v] = u] = newLabel;
    }
    int merge(int u, int v){ merge(u, v, u); return u; }
    int count(){ return N; }
    int size(int u){ return -P[leader(u)]; }
    bool same(int u, int v){ return leader(u) == leader(v); }
};

} // namespace nachia


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
using namespace std;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;

using Modint = atcoder::static_modint<998244353>;

int main(){
    int N, Q; cin >> N >> Q;
    auto dsu = nachia::Dsu(N+10);
    int pre[26] = {};
    int p10 = N;
    rep(q,Q){
        string S; cin >> S;
        rep(i,26) pre[i] = -1;
        rep(i,S.size()){
            if(S[i] == '?') continue;
            if('0' <= S[i] && S[i] <= '9'){
                int u = i;
                int v = N + S[i] - '0';
                if(dsu.same(u, v)) continue;
                dsu.merge(u, v);
                p10--;
            }
            else{
                int u = pre[S[i] - 'a'];
                int v = i;
                pre[S[i] - 'a'] = i;
                if(u == -1) continue;
                if(dsu.same(u, v)) continue;
                dsu.merge(u, v);
                p10--;
            }
        }
        bool f = true;
        rep(i,10) rep(j,i) if(dsu.same(N+i, N+j)) f = false;
        if(f){
            cout << Modint(10).pow(p10).val() << '\n';
        }
        else{
            cout << "0\n";
        }
    }
    return 0;
}



struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;

0