結果

問題 No.1669 パズル作成
ユーザー t33ft33f
提出日時 2021-09-04 01:34:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 1,661 bytes
コンパイル時間 1,013 ms
コンパイル使用メモリ 86,560 KB
実行使用メモリ 5,244 KB
最終ジャッジ日時 2023-08-22 04:30:14
合計ジャッジ時間 4,139 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 37 ms
4,760 KB
testcase_07 AC 102 ms
4,384 KB
testcase_08 AC 55 ms
5,096 KB
testcase_09 AC 91 ms
4,384 KB
testcase_10 AC 56 ms
4,380 KB
testcase_11 AC 72 ms
4,380 KB
testcase_12 AC 55 ms
5,244 KB
testcase_13 AC 54 ms
5,072 KB
testcase_14 AC 56 ms
5,080 KB
testcase_15 AC 63 ms
4,380 KB
testcase_16 AC 87 ms
4,380 KB
testcase_17 AC 20 ms
4,380 KB
testcase_18 AC 93 ms
4,380 KB
testcase_19 AC 91 ms
4,380 KB
testcase_20 AC 81 ms
4,384 KB
testcase_21 AC 72 ms
4,380 KB
testcase_22 AC 63 ms
4,380 KB
testcase_23 AC 55 ms
4,380 KB
testcase_24 AC 46 ms
4,380 KB
testcase_25 AC 38 ms
4,380 KB
testcase_26 AC 19 ms
4,380 KB
testcase_27 AC 86 ms
4,384 KB
testcase_28 AC 77 ms
4,384 KB
testcase_29 AC 23 ms
4,380 KB
testcase_30 AC 29 ms
4,556 KB
testcase_31 AC 30 ms
4,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <vector>
#include <set>
#include <iostream>
using namespace std;
int main() {
    int n, m; cin >> n >> m;
    vector<vector<int> > adj(2*n);
    for (int i = 0; i < m; i++) {
        int r, c; cin >> r >> c;
        r--; c--; c += n;
        adj[r].push_back(c);
        adj[c].push_back(r);
    }
    vector<pair<int, int> > AB;
    struct DFS {
        int n;
        vector<bool> visited;
        vector<vector<int> > &adj;
        DFS(int n, vector<vector<int> > &adj) : n(n), visited(2*n, false), adj(adj) {}
        void dfs(int i, int &a, int &b) {
            visited[i] = true;
            if (i < n) a++; else b++;
            for (int j : adj[i]) if (!visited[j]) dfs(j, a, b);
        }
        pair<int, int> dfs(int i) {
            int a = 0, b = 0;
            dfs(i, a, b);
            return {a, b};
        }
    } dfs(n, adj);
    for (int i = 0; i < 2*n; i++) {
        if (!dfs.visited[i]) {
            auto [a, b] = dfs.dfs(i);
            AB.emplace_back(a, b);
        }
    }

    constexpr int INF = 1 << 30;
    vector<int> bmin(n+1, INF), bmax(n+1, -INF);
    bmin[0] = bmax[0] = 0;
    for (auto [a, b] : AB) {
        for (int i = n - a; i >= 0; i--) {
            if (bmin[i] == INF) { assert(bmax[i] == -INF); continue; }
            bmin[i + a] = min(bmin[i + a], bmin[i] + b);
            bmax[i + a] = max(bmax[i + a], bmax[i] + b);
        }
    }
    int ans = n * n;
    for (int a = 0; a <= n; a++) {
        if (bmin[a] != INF) {
            const int b = 2 * a <= n ? bmax[a] : bmin[a];
            ans = min(ans, (n - b) * (n - a) + a * b);
        }
    }
    cout << ans - m << '\n';
}
0