結果

問題 No.2251 Marking Grid
ユーザー suisensuisen
提出日時 2023-05-11 17:46:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,065 ms / 3,000 ms
コード長 1,562 bytes
コンパイル時間 1,315 ms
コンパイル使用メモリ 104,420 KB
実行使用メモリ 74,496 KB
最終ジャッジ日時 2024-05-05 15:59:45
合計ジャッジ時間 8,715 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 236 ms
19,072 KB
testcase_23 AC 273 ms
19,712 KB
testcase_24 AC 13 ms
5,376 KB
testcase_25 AC 277 ms
19,712 KB
testcase_26 AC 66 ms
8,320 KB
testcase_27 AC 155 ms
13,056 KB
testcase_28 AC 12 ms
5,376 KB
testcase_29 AC 164 ms
14,208 KB
testcase_30 AC 56 ms
7,936 KB
testcase_31 AC 39 ms
6,656 KB
testcase_32 AC 34 ms
8,960 KB
testcase_33 AC 1,065 ms
74,496 KB
testcase_34 AC 48 ms
10,880 KB
testcase_35 AC 409 ms
38,400 KB
testcase_36 AC 571 ms
47,872 KB
testcase_37 AC 849 ms
63,104 KB
testcase_38 AC 19 ms
6,656 KB
testcase_39 AC 11 ms
5,376 KB
testcase_40 AC 373 ms
35,456 KB
testcase_41 AC 132 ms
19,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>

#include <atcoder/dsu>
#include <atcoder/modint>

using mint = atcoder::modint998244353;

namespace atcoder {
    std::istream& operator>>(std::istream& in, mint& a) {
        long long e; in >> e; a = e;
        return in;
    }

    std::ostream& operator<<(std::ostream& out, const mint& a) {
        out << a.val();
        return out;
    }
} // namespace atcoder

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m;
    std::cin >> n >> m;

    std::vector<std::vector<int>> a(n, std::vector<int>(m));
    std::map<int, std::vector<std::pair<int, int>>, std::greater<int>> pos;

    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
        std::cin >> a[i][j];
        pos[a[i][j]].emplace_back(i, j);
    }

    mint ans = 0;

    atcoder::dsu uf_row(n), uf_col(m);
    int t = n + m - 1;
    std::vector<int> ng_row(n, -1), ng_col(m, -1);

    for (auto [v, p] : pos) {
        for (auto [i, j] : p) {
            int j2 = ng_row[i];
            int i2 = ng_col[j];
            if ((i2 == -1 or not uf_row.same(i, i2)) and (j2 == -1 or not uf_col.same(j, j2))) {
                ans += v * mint(2).pow(t - 1);
                --t;
            }

            if (j2 != -1) {
                uf_col.merge(j, j2);
            } else {
                ng_row[i] = j;
            }

            if (i2 != -1) {
                uf_row.merge(i, i2);
            } else {
                ng_col[j] = i;
            }
        }
    }

    std::cout << ans.val() << std::endl;
}
0