結果

問題 No.2291 Union Find Estimate
ユーザー SSRSSSRS
提出日時 2023-05-05 21:33:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 1,409 bytes
コンパイル時間 1,933 ms
コンパイル使用メモリ 169,532 KB
実行使用メモリ 9,172 KB
最終ジャッジ日時 2023-08-15 03:09:42
合計ジャッジ時間 3,112 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long ONE_TENTH = 299473306;
struct unionfind{
  vector<int> p;
  long long ans;
  vector<int> s;
  unionfind(int N): p(N, -1), ans(1), s(N, -1){
    for (int i = 0; i < N; i++){
      ans *= 10;
      ans %= MOD;
    }
  }
  int root(int x){
    if (p[x] == -1){
      return x;
    } else {
      p[x] = root(p[x]);
      return p[x];
    }
  }
  void set(int x, int y){
    x = root(x);
    if (s[x] == -1){
      s[x] = y;
      ans *= ONE_TENTH;
      ans %= MOD;
    } else if (s[x] != y){
      ans = 0;
    }
  }
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if (x != y){
      p[x] = y;
      if (s[x] == -1 || s[y] == -1){
        ans *= ONE_TENTH;
        ans %= MOD;
        if (s[y] == -1){
          s[y] = s[x];
        }
      } else if (s[x] != s[y]){
        ans = 0;
      }
    }
  }
};
int main(){
  int W, H;
  cin >> W >> H;
  vector<string> Q(H);
  for (int i = 0; i < H; i++){
    cin >> Q[i];
  }
  unionfind UF(W);
  for (int i = 0; i < H; i++){
    vector<int> p(26, -1);
    for (int j = 0; j < W; j++){
      if (isdigit(Q[i][j]) > 0){
        UF.set(j, Q[i][j] - '0');
      } else if (isalpha(Q[i][j]) > 0){
        int c = Q[i][j] - 'a';
        if (p[c] != -1){
          UF.unite(p[c], j);
        }
        p[c] = j;
      }
    }
    cout << UF.ans << endl;
  }
}
0