結果

問題 No.2291 Union Find Estimate
ユーザー t98slidert98slider
提出日時 2023-03-16 19:14:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,631 bytes
コンパイル時間 4,836 ms
コンパイル使用メモリ 233,436 KB
実行使用メモリ 5,200 KB
最終ジャッジ日時 2023-10-18 13:08:36
合計ジャッジ時間 6,042 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

struct Problem_C{
    int num_component;
    bool is_zero = false;
    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, y;
        tie(y, x) = minmax(leader(u), leader(v));
        if(x == y) return;
        if(y >= parent_or_size.size() - 10) is_zero = true;
        else if(x < parent_or_size.size() - 10 && -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;
        }

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