結果
問題 | No.2291 Union Find Estimate |
ユーザー | aaaaaaaaaa2230 |
提出日時 | 2023-05-05 21:59:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 484 ms / 2,000 ms |
コード長 | 1,758 bytes |
コンパイル時間 | 165 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 77,568 KB |
最終ジャッジ日時 | 2024-11-23 07:33:37 |
合計ジャッジ時間 | 3,368 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
class Unionfind: def __init__(self,n): self.uf = [-1]*n self.ps = [-2]*n def find(self,x): if self.uf[x] < 0: return x else: self.uf[x] = self.find(self.uf[x]) return self.uf[x] def same(self,x,y): return self.find(x) == self.find(y) def update(self,x,nx): x = self.find(x) ny = self.ps[x] if nx != ny: if nx != -2 and ny != -2: self.ps[x] = -1 else: self.ps[x] = max(nx,ny) def union(self,x,y): x = self.find(x) y = self.find(y) nx = self.ps[x] ny = self.ps[y] if x == y: return False if self.uf[x] > self.uf[y]: x,y = y,x self.uf[x] += self.uf[y] self.uf[y] = x if nx != ny: if nx != -2 and ny != -2: self.ps[x] = -1 else: self.ps[x] = max(nx,ny) return True def size(self,x): x = self.find(x) return -self.uf[x] w,h = map(int,input().split()) mod = 998244353 uf = Unionfind(w) for i in range(h): q = input() last = [-1]*26 for j in range(w): s = q[j] if "0" <= s <= "9": uf.update(j,int(s)) elif "a" <= s <= "z": pos = ord(s)-ord("a") if last[pos] == -1: last[pos] = j else: uf.union(last[pos],j) ans = 1 for j in range(w): if uf.find(j) == j: num = uf.ps[j] if num == -2: ans *= 10 elif num == -1: ans = 0 break ans %= mod print(ans)