結果

問題 No.2884 Pieces on Squares
ユーザー ooaiu
提出日時 2025-09-03 18:23:45
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 1,160 bytes
コンパイル時間 3,713 ms
コンパイル使用メモリ 291,800 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2025-09-03 18:23:57
合計ジャッジ時間 10,579 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 19 RE * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/dsu>
using namespace std;
using ll = long long;
int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int h, w, n;
    cin >> h >> w >> n;
    atcoder::dsu uf(h + w);
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        a[i]--, b[i]--;
        uf.merge(a[i], b[i] + h);
    }
    vector dp(h + 1, vector<bool>(w + 1));
    dp[0][0] = 1;
    auto G = uf.groups();
    const int K = G.size();
    assert((ll)K * h * w <= 2e6);
    for (int i = 0; i < K; i++) {
        int r = 0, c = 0;
        for (int x : G[i]) {
            r += x < h;
            c += x >= h;
        }
        vector ndp = dp;
        for (int j = 0; j <= h; j++) {
            for (int k = 0; k <= w; k++) {
                if (dp[j][k]) {
                    ndp[j + r][k + c] = 1;
                }
            }
        }
        dp = std::move(ndp);
    }
    ll ans = 0;
    for (int i = 0; i <= h; i++) {
        for (int j = 0; j <= w; j++) {
            if (dp[i][j]) ans = max<ll>(ans, i * w + j * h - 2 * i * j);
        }
    }
    cout << ans << "\n";
}
0