結果
問題 | No.2251 Marking Grid |
ユーザー |
![]() |
提出日時 | 2023-03-17 23:45:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 433 ms / 3,000 ms |
コード長 | 890 bytes |
コンパイル時間 | 215 ms |
コンパイル使用メモリ | 82,356 KB |
実行使用メモリ | 178,048 KB |
最終ジャッジ日時 | 2024-09-18 12:38:50 |
合計ジャッジ時間 | 6,647 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 40 |
ソースコード
class UnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x mod = 998244353 h,w = map(int,input().split()) a = [list(map(int,input().split())) for i in range(h)] v = [] for i in range(h): for j in range(w): v.append((a[i][j] << 30) + (i << 15) + j) v.sort() mask = (1 << 15) - 1 uf = UnionFind(w + h) cnt = w + h - 2 ans = 0 for ty in v[::-1]: j = ty & mask i = (ty >> 15) & mask val = ty >> 30 if uf.find(j) == uf.find(i+w): continue uf.union(j,i+w) ans += pow(2, cnt, mod) * val ans %= mod cnt -= 1 print(ans)