結果
問題 | No.2291 Union Find Estimate |
ユーザー | qib |
提出日時 | 2023-05-06 04:13:23 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 534 ms / 2,000 ms |
コード長 | 2,068 bytes |
コンパイル時間 | 289 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 94,884 KB |
最終ジャッジ日時 | 2024-05-02 21:01:22 |
合計ジャッジ時間 | 4,273 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
52,736 KB |
testcase_01 | AC | 43 ms
52,480 KB |
testcase_02 | AC | 534 ms
77,620 KB |
testcase_03 | AC | 140 ms
79,744 KB |
testcase_04 | AC | 64 ms
63,104 KB |
testcase_05 | AC | 61 ms
62,336 KB |
testcase_06 | AC | 54 ms
55,296 KB |
testcase_07 | AC | 49 ms
54,144 KB |
testcase_08 | AC | 115 ms
76,672 KB |
testcase_09 | AC | 114 ms
76,544 KB |
testcase_10 | AC | 176 ms
76,824 KB |
testcase_11 | AC | 200 ms
77,220 KB |
testcase_12 | AC | 248 ms
77,440 KB |
testcase_13 | AC | 143 ms
94,884 KB |
testcase_14 | AC | 234 ms
76,800 KB |
testcase_15 | AC | 195 ms
78,464 KB |
testcase_16 | AC | 240 ms
77,312 KB |
testcase_17 | AC | 135 ms
79,156 KB |
testcase_18 | AC | 131 ms
78,756 KB |
testcase_19 | AC | 167 ms
77,232 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)