結果

問題 No.2251 Marking Grid
コンテスト
ユーザー sotanishy
提出日時 2023-03-17 23:08:29
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,665 ms / 3,000 ms
コード長 1,151 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 308 ms
コンパイル使用メモリ 85,120 KB
実行使用メモリ 272,940 KB
最終ジャッジ日時 2026-04-06 16:27:50
合計ジャッジ時間 14,020 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, N):
        self.par = [-1] * N

    def find(self, x):
        r = x
        while self.par[r] >= 0:
            r = self.par[r]
        while x != r:
            tmp = self.par[x]
            self.par[x] = r
            x = tmp
        return r

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.par[x] > self.par[y]:
            x, y = y, x
        self.par[x] += self.par[y]
        self.par[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.par[self.find(x)]


mod = 998244353
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
vals = []
for i in range(H):
    for j in range(W):
        vals.append((A[i][j], i, j))

vals.sort(key=lambda p: -p[0])
uf = UnionFind(H+W)
comp = H + W
ans = 0

for x, i, j in vals:
    if uf.same(i, H+j):
        continue
    ans = (ans + x * pow(2, comp - 1, mod)) % mod
    uf.unite(i, H+j)
    comp -= 1
print(ans * pow(2, mod-2, mod) % mod)
0