結果

問題 No.2251 Marking Grid
ユーザー sotanishysotanishy
提出日時 2023-03-17 23:08:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,964 ms / 3,000 ms
コード長 1,151 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 81,772 KB
実行使用メモリ 269,080 KB
最終ジャッジ日時 2023-10-18 16:07:54
合計ジャッジ時間 21,186 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,572 KB
testcase_01 AC 37 ms
53,572 KB
testcase_02 AC 37 ms
53,572 KB
testcase_03 AC 37 ms
53,572 KB
testcase_04 AC 37 ms
53,572 KB
testcase_05 AC 37 ms
53,572 KB
testcase_06 AC 37 ms
53,572 KB
testcase_07 AC 38 ms
53,572 KB
testcase_08 AC 37 ms
53,572 KB
testcase_09 AC 39 ms
53,572 KB
testcase_10 AC 38 ms
53,572 KB
testcase_11 AC 38 ms
53,572 KB
testcase_12 AC 38 ms
53,572 KB
testcase_13 AC 38 ms
53,572 KB
testcase_14 AC 38 ms
53,572 KB
testcase_15 AC 38 ms
53,572 KB
testcase_16 AC 37 ms
53,572 KB
testcase_17 AC 38 ms
53,572 KB
testcase_18 AC 38 ms
53,572 KB
testcase_19 AC 38 ms
53,572 KB
testcase_20 AC 38 ms
53,572 KB
testcase_21 AC 37 ms
53,572 KB
testcase_22 AC 2,047 ms
223,864 KB
testcase_23 AC 2,964 ms
269,080 KB
testcase_24 AC 109 ms
81,312 KB
testcase_25 AC 2,914 ms
268,876 KB
testcase_26 AC 532 ms
117,752 KB
testcase_27 AC 1,335 ms
177,992 KB
testcase_28 AC 101 ms
79,892 KB
testcase_29 AC 1,408 ms
173,432 KB
testcase_30 AC 493 ms
117,028 KB
testcase_31 AC 295 ms
98,032 KB
testcase_32 AC 141 ms
83,040 KB
testcase_33 AC 1,659 ms
186,752 KB
testcase_34 AC 171 ms
87,724 KB
testcase_35 AC 755 ms
130,244 KB
testcase_36 AC 897 ms
139,912 KB
testcase_37 AC 1,326 ms
160,644 KB
testcase_38 AC 104 ms
80,400 KB
testcase_39 AC 84 ms
77,808 KB
testcase_40 AC 655 ms
121,448 KB
testcase_41 AC 328 ms
99,780 KB
権限があれば一括ダウンロードができます

ソースコード

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