結果

問題 No.2291 Union Find Estimate
ユーザー t98slidert98slider
提出日時 2023-02-15 14:12:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,667 bytes
コンパイル時間 3,680 ms
コンパイル使用メモリ 231,016 KB
実行使用メモリ 5,052 KB
最終ジャッジ日時 2023-09-24 23:29:11
合計ジャッジ時間 5,391 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using mint = atcoder::modint998244353;

struct Problem_C{
    int num_component;
    vector<int> parent_or_size;

    Problem_C(int N) : num_component(N), parent_or_size(N, -1) {}

    int leader(int v){
        if(parent_or_size[v] < 0) return v;
        return parent_or_size[v] = leader(parent_or_size[v]);
    }

    bool same(int u, int v) { return leader(u) == leader(v); }

    void merge(int u, int v){
        int x = leader(u), y = leader(v);
        if(x == y) return;
        if(-parent_or_size[x] < parent_or_size[y]) swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        num_component--;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int W, H;
    cin >> W >> H;

    vector<mint> pow10(W + 1);
    pow10[0] = 1;
    for(int i = 0; i < W; i++)pow10[i + 1] = pow10[i] * 10;

    Problem_C uf(W + 10);

    while(H--){
        string s;
        cin >> s;

        array<int, 26> pos{};
        pos.fill(-1);
        
        for(int i = 0; i < W; i++){
            if(s[i] == '?') continue;
            if('0' <= s[i] && s[i] <= '9') {
                uf.merge(s[i] - '0' + W, i);
                continue;
            }
            int c = s[i] - 'a';
            if(pos[c] != -1) uf.merge(i, pos[c]);
            pos[c] = i;
        }

        bool is_zero = false;
        for(int i = 0; i < 10; i++){
            for(int j = 0; j < i; j++){
                if(uf.same(i + W, j + W)) is_zero = true;
            }
        }

        cout << (is_zero ? 0 : pow10[uf.num_component - 10].val()) << '\n';
    }
}
0