結果

問題 No.1669 パズル作成
ユーザー ぷらぷら
提出日時 2021-09-19 21:30:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,274 bytes
コンパイル時間 2,560 ms
コンパイル使用メモリ 214,624 KB
実行使用メモリ 364,952 KB
最終ジャッジ日時 2023-09-14 18:55:44
合計ジャッジ時間 8,885 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 3 ms
4,628 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,500 KB
testcase_06 AC 34 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 45 ms
4,376 KB
testcase_09 AC 394 ms
356,000 KB
testcase_10 AC 224 ms
199,312 KB
testcase_11 AC 309 ms
277,564 KB
testcase_12 AC 45 ms
4,376 KB
testcase_13 AC 46 ms
4,376 KB
testcase_14 AC 45 ms
4,376 KB
testcase_15 AC 266 ms
237,704 KB
testcase_16 AC 377 ms
340,912 KB
testcase_17 WA -
testcase_18 AC 400 ms
364,952 KB
testcase_19 AC 392 ms
356,104 KB
testcase_20 AC 347 ms
316,580 KB
testcase_21 AC 307 ms
277,508 KB
testcase_22 AC 263 ms
238,292 KB
testcase_23 AC 222 ms
199,104 KB
testcase_24 AC 185 ms
162,332 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 366 ms
336,476 KB
testcase_28 AC 323 ms
297,256 KB
testcase_29 WA -
testcase_30 AC 24 ms
4,380 KB
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct UnionFind {
    vector<int> par;
    vector<int> size;
    UnionFind(int n) {
        par.resize(n);
        size.resize(n,1);
        for(int i = 0; i < n; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        }
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return;
        }
        if(size[x] < size[y]) {
            par[x] = y;
            size[y] += size[x];
        }
        else {
            par[y] = x;
            size[x] += size[y];
        }
    }
};

int inf = 1001001001;

int main() {
    int N,M;
    cin >> N >> M;
    if(M == 0) {
        cout << 0 << endl;
        return 0;
    }
    UnionFind uf(2*N);
    for(int i = 0; i < M; i++) {
        int r,c;
        cin >> r >> c;
        r--;
        c--;
        uf.unite(r,N+c);
    }
    map<int,pair<int,int>>tmp;
    for(int i = 0; i < 2*N; i++) {
        if(i < N) {
            tmp[uf.find(i)].first++;
        }
        else {
            tmp[uf.find(i)].second++;
        }
    }
    int n = tmp.size();
    vector<vector<int>>dp1(n+1,vector<int>(N+1,-inf)),dp2(n+1,vector<int>(N+1,inf));
    dp1[0][0] = 0;
    dp2[0][0] = 0;
    int k = 0;
    for(auto x:tmp) {
        for(int j = 0; j <= N; j++) {
            dp1[k+1][j] = max(dp1[k+1][j],dp1[k][j]);
            dp2[k+1][j] = min(dp2[k+1][j],dp2[k][j]);
            if(j+x.second.first <= N) {
                dp1[k+1][j+x.second.first] = max(dp1[k+1][j+x.second.first],dp1[k][j]+x.second.second);
                dp2[k+1][j+x.second.first] = min(dp2[k+1][j+x.second.first],dp2[k][j]+x.second.second);
            }
        }
        k++;
    }
    int ans = inf;
    for(int i = 0; i <= N; i++) {
        if(dp1[n][i] != -inf) {
            int a = i,b = dp1[n][i];
            ans = min(ans,a*b+(N-a)*(N-b)-M);
        }
        if(dp2[n][i] != inf) {
            int a = i,b = dp2[n][i];
            ans = min(ans,a*b+(N-a)*(N-b)-M);
        }
    }
    cout << ans << endl;
}
0