結果

問題 No.2291 Union Find Estimate
ユーザー ぷらぷら
提出日時 2023-05-05 21:31:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 2,270 bytes
コンパイル時間 2,821 ms
コンパイル使用メモリ 213,744 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2023-08-15 03:04:31
合計ジャッジ時間 3,649 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct UnionFind {
    vector<int> par;
    vector<int> size;
    UnionFind(int n) {
        par.resize(n);
        size.resize(n,1);
        for(int i = 0; i < n; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        }
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return false;
        }
        if(size[x] > size[y]) {
            swap(x,y);
        }
        par[x] = y;
        size[y] += size[x];
        return true;
    }
};

constexpr int mod = 998244353;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int W,H;
    cin >> W >> H;
    vector<int>tmp(W,-1);
    UnionFind uf(W);
    bool f = true;
    for(int i = 0; i < H; i++) {
        string Q;
        cin >> Q;
        vector<vector<int>>id(26);
        for(int j = 0; j < W; j++) {
            if(Q[j] == '?') continue;
            if(Q[j] >= '0' && Q[j] <= '9') {
                int c = Q[j]-'0';
                if(tmp[j] == -1) tmp[j] = c;
                else if(tmp[j] != c) f = false;
            }
            else {
                id[Q[j]-'a'].push_back(j);
            }
        }
        for(int j = 0; j < 26; j++) {
            for(int k = 1; k < id[j].size(); k++) {
                uf.unite(id[j][k-1],id[j][k]);
            }
        }
        map<int,int>mp;
        for(int j = 0; j < W; j++) {
            int c = uf.find(j);
            int d = tmp[j];
            if(!mp.count(c)) {
                mp[c] = d;
            }
            else {
                if(mp[c] == -1) {
                    mp[c] = d;
                }
                else if(d != -1 && mp[c] != d) {
                    f = false;
                }
            }
        }
        if(!f) {
            cout << 0 << "\n";
            continue;
        }
        int ans = 1;
        for(auto j:mp) {
            if(j.second == -1) {
                ans = 10ll*ans%mod;
            }
        }
        cout << ans << "\n";
    }
}
0