結果
問題 | No.2291 Union Find Estimate |
ユーザー | qib |
提出日時 | 2023-05-06 04:13:23 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 504 ms / 2,000 ms |
コード長 | 2,068 bytes |
コンパイル時間 | 295 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 94,756 KB |
最終ジャッジ日時 | 2024-11-23 15:32:50 |
合計ジャッジ時間 | 3,956 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
52,352 KB |
testcase_01 | AC | 41 ms
52,352 KB |
testcase_02 | AC | 504 ms
77,368 KB |
testcase_03 | AC | 129 ms
79,496 KB |
testcase_04 | AC | 57 ms
62,976 KB |
testcase_05 | AC | 57 ms
62,336 KB |
testcase_06 | AC | 49 ms
55,808 KB |
testcase_07 | AC | 45 ms
53,888 KB |
testcase_08 | AC | 102 ms
76,416 KB |
testcase_09 | AC | 103 ms
76,416 KB |
testcase_10 | AC | 156 ms
76,696 KB |
testcase_11 | AC | 184 ms
76,948 KB |
testcase_12 | AC | 234 ms
76,928 KB |
testcase_13 | AC | 128 ms
94,756 KB |
testcase_14 | AC | 214 ms
76,800 KB |
testcase_15 | AC | 180 ms
78,208 KB |
testcase_16 | AC | 216 ms
76,928 KB |
testcase_17 | AC | 124 ms
78,896 KB |
testcase_18 | AC | 120 ms
78,540 KB |
testcase_19 | AC | 154 ms
77,108 KB |
ソースコード
MOD = 998244353 class UnionFind: def __init__(self, n): self.node = [-1 for _ in range(n)] def root(self, v): if self.node[v] < 0: return v st = [] while self.node[v] >= 0: st.append(v) v = self.node[v] for u in st: self.node[u] = v return v def size(self, v): v = self.root(v) return (- self.node[v]) def same(self, u, v): return self.root(u) == self.root(v) def unite(self, u, v): ru = self.root(u) rv = self.root(v) if ru == rv: return du = self.node[ru] dv = self.node[rv] if du <= dv: self.node[rv] = ru self.node[ru] += dv else: self.node[ru] = rv self.node[rv] += du w, h = map(int, input().split()) uf = UnionFind(w) num = [None for _ in range(w)] flg = True for _ in range(h): qi = input() ans = 1 if not flg: ans = 0 idx = {} for j in range(w * ans): if qi[j] == "?": continue elif ord("a") <= ord(qi[j]) <= ord("z"): if idx.get(qi[j], None) is None: idx[qi[j]] = j else: uf.unite(idx[qi[j]], j) r = uf.root(j) none_r = num[r] is None none_j = num[j] is None if not none_r and not none_j: if num[r] != num[j]: flg = False ans = 0 break elif none_r and not none_j: num[r] = num[j] elif none_j and not none_r: num[j] = num[r] else: if num[j] is None: num[j] = int(qi[j]) elif num[j] != int(qi[j]): flg = False ans = 0 break par = {} for v in range(w * ans): r = uf.root(v) none_r = num[r] is None none_v = num[v] is None if not none_r and not none_v: if num[r] != num[v]: flg = False ans = 0 break elif none_r and not none_v: num[r] = num[v] elif none_v and not none_r: num[v] = num[r] if par.get(r, None) is None: par[r] = num[r] for k, v in par.items(): if v is None: ans = (ans * 10) % MOD print(ans)