結果
問題 | No.2291 Union Find Estimate |
ユーザー |
|
提出日時 | 2023-05-05 21:35:34 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 226 ms / 2,000 ms |
コード長 | 2,463 bytes |
コンパイル時間 | 248 ms |
コンパイル使用メモリ | 82,000 KB |
実行使用メモリ | 93,184 KB |
最終ジャッジ日時 | 2024-11-23 06:19:35 |
合計ジャッジ時間 | 3,738 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
import sys,random,bisect from collections import deque,defaultdict from heapq import heapify,heappop,heappush from itertools import permutations from math import gcd,log input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) class UnionFindVerSize(): def __init__(self, N): self._parent = [n for n in range(0, N)] self._size = [1] * N self.group = N def find_root(self, x): if self._parent[x] == x: return x self._parent[x] = self.find_root(self._parent[x]) return self._parent[x] def unite(self, x, y): gx = self.find_root(x) gy = self.find_root(y) if gx == gy: return self.group -= 1 if self._size[gx] < self._size[gy]: self._parent[gx] = gy self._size[gy] += self._size[gx] else: self._parent[gy] = gx self._size[gx] += self._size[gy] def get_size(self, x): return self._size[self.find_root(x)] def is_same_group(self, x, y): return self.find_root(x) == self.find_root(y) ppp = ord("a") num = ord("0") W,H = mi() uf = UnionFindVerSize(W) det = [-1] * W k = W contra = 0 for _ in range(H): S = input() tmp = [[] for i in range(26)] for i,s in enumerate(S): if ppp <= ord(s) < ppp+26: tmp[ord(s)-ppp].append(i) for i in range(26): for a,b in zip(tmp[i],tmp[i][1:]): if not uf.is_same_group(a,b): pa,pb = uf.find_root(a),uf.find_root(b) if det[pa]!=-1 and det[pb]!=-1 and det[pa]!=det[pb]: contra = 1 else: if det[pa] == -1: k -= 1 if det[pb] == -1: k -= 1 uf.unite(pa,pb) pc = uf.find_root(pa) if det[pa] == -1: det[pc] = det[pb] else: det[pc] = det[pa] if det[pc] == -1: k += 1 for i,s in enumerate(S): if num <= ord(s) < num+10: s = int(s) pi = uf.find_root(i) if det[pi] == -1: k -= 1 det[pi] = s elif det[pi]!=s: contra = 1 if contra: print(0) else: print(pow(10,k,998244353))