結果

問題 No.2251 Marking Grid
ユーザー suisen
提出日時 2023-05-11 17:46:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,444 ms / 3,000 ms
コード長 1,562 bytes
コンパイル時間 1,316 ms
コンパイル使用メモリ 99,068 KB
最終ジャッジ日時 2025-02-12 21:22:25
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

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