結果

問題 No.1669 パズル作成
ユーザー ぷらぷら
提出日時 2021-09-19 21:33:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 403 ms / 2,000 ms
コード長 2,271 bytes
コンパイル時間 2,795 ms
コンパイル使用メモリ 214,576 KB
実行使用メモリ 365,044 KB
最終ジャッジ日時 2023-09-14 19:01:16
合計ジャッジ時間 9,359 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,616 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 34 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 45 ms
4,380 KB
testcase_09 AC 399 ms
356,144 KB
testcase_10 AC 224 ms
199,272 KB
testcase_11 AC 308 ms
277,564 KB
testcase_12 AC 46 ms
4,376 KB
testcase_13 AC 46 ms
4,376 KB
testcase_14 AC 45 ms
4,380 KB
testcase_15 AC 267 ms
237,580 KB
testcase_16 AC 375 ms
341,084 KB
testcase_17 AC 80 ms
67,160 KB
testcase_18 AC 403 ms
365,044 KB
testcase_19 AC 390 ms
356,020 KB
testcase_20 AC 350 ms
316,640 KB
testcase_21 AC 311 ms
277,564 KB
testcase_22 AC 266 ms
238,656 KB
testcase_23 AC 226 ms
199,128 KB
testcase_24 AC 184 ms
162,244 KB
testcase_25 AC 150 ms
130,084 KB
testcase_26 AC 81 ms
65,900 KB
testcase_27 AC 371 ms
336,148 KB
testcase_28 AC 329 ms
297,240 KB
testcase_29 AC 97 ms
83,052 KB
testcase_30 AC 24 ms
4,380 KB
testcase_31 AC 24 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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] >= 0) {
            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