結果

問題 No.2251 Marking Grid
ユーザー sotanishy
提出日時 2023-03-17 23:08:29
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,151 bytes
コンパイル時間 243 ms
コンパイル使用メモリ 81,836 KB
実行使用メモリ 269,576 KB
最終ジャッジ日時 2024-09-18 12:14:11
合計ジャッジ時間 21,713 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 39 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

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