結果
問題 | No.2291 Union Find Estimate |
ユーザー | rlangevin |
提出日時 | 2023-05-05 22:21:13 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 231 ms / 2,000 ms |
コード長 | 2,656 bytes |
コンパイル時間 | 550 ms |
コンパイル使用メモリ | 86,944 KB |
実行使用メモリ | 107,636 KB |
最終ジャッジ日時 | 2023-08-15 04:46:46 |
合計ジャッジ時間 | 4,524 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 76 ms
71,180 KB |
testcase_01 | AC | 76 ms
71,144 KB |
testcase_02 | AC | 231 ms
78,364 KB |
testcase_03 | AC | 139 ms
107,636 KB |
testcase_04 | AC | 156 ms
77,712 KB |
testcase_05 | AC | 155 ms
77,796 KB |
testcase_06 | AC | 142 ms
78,104 KB |
testcase_07 | AC | 130 ms
77,732 KB |
testcase_08 | AC | 124 ms
77,636 KB |
testcase_09 | AC | 122 ms
77,644 KB |
testcase_10 | AC | 131 ms
77,720 KB |
testcase_11 | AC | 140 ms
78,000 KB |
testcase_12 | AC | 170 ms
78,800 KB |
testcase_13 | AC | 120 ms
82,516 KB |
testcase_14 | AC | 131 ms
77,576 KB |
testcase_15 | AC | 162 ms
91,932 KB |
testcase_16 | AC | 144 ms
79,248 KB |
testcase_17 | AC | 129 ms
79,004 KB |
testcase_18 | AC | 130 ms
79,264 KB |
testcase_19 | AC | 113 ms
77,596 KB |
ソースコード
import sys input = sys.stdin.readline class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.rank[x] < self.rank[y]: x, y = y, x if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.size[x] += self.size[y] def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] W, H = map(int, input().split()) mod = 998244353 U = UnionFind(W) num = list(range(10)) num = set(list(map(str, num))) numlst = [-1] * W flag = 1 ans = pow(10, W, mod) invten = [1] * (W + 1) inv = pow(10, mod - 2, mod) for i in range(W): invten[i + 1] = invten[i] * inv invten[i + 1] %= mod for _ in range(H): S = list(input().rstrip("\n")) L = [[] for i in range(26)] cnt = 0 for i, s in enumerate(S): if s == "?": pass elif s in num: s = int(s) if numlst[U.find(i)] == -1: cnt += 1 numlst[U.find(i)] = s elif numlst[U.find(i)] != s: flag = 0 else: L[ord(s) - ord("a")].append(i) for j in range(26): if len(L[j]) <= 1: continue else: for k in range(1, len(L[j])): if U.is_same(L[j][0], L[j][k]): continue else: cnt += 1 Lj0 = U.find(L[j][0]) Ljk = U.find(L[j][k]) if numlst[Lj0] != numlst[Ljk]: if numlst[Lj0] != -1 and numlst[Ljk] != -1: flag = 0 else: mx = max(numlst[Lj0], numlst[Ljk]) U.union(Lj0, Ljk) numlst[U.find(Lj0)] = mx else: if numlst[Lj0] != -1: cnt -= 1 mx = max(numlst[Lj0], numlst[Ljk]) U.union(Lj0, Ljk) numlst[U.find(Lj0)] = mx ans *= invten[cnt] ans %= mod if flag: print(ans) else: print(0)