結果

問題 No.1669 パズル作成
ユーザー SSRSSSRS
提出日時 2021-09-03 22:43:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 1,802 bytes
コンパイル時間 1,857 ms
コンパイル使用メモリ 178,252 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-05-09 05:43:38
合計ジャッジ時間 7,528 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 41 ms
5,376 KB
testcase_07 AC 339 ms
5,376 KB
testcase_08 AC 58 ms
5,888 KB
testcase_09 AC 310 ms
5,376 KB
testcase_10 AC 193 ms
5,376 KB
testcase_11 AC 255 ms
5,376 KB
testcase_12 AC 59 ms
6,016 KB
testcase_13 AC 58 ms
6,016 KB
testcase_14 AC 58 ms
6,016 KB
testcase_15 AC 224 ms
5,376 KB
testcase_16 AC 299 ms
5,376 KB
testcase_17 AC 54 ms
5,376 KB
testcase_18 AC 316 ms
5,376 KB
testcase_19 AC 312 ms
5,376 KB
testcase_20 AC 284 ms
5,376 KB
testcase_21 AC 253 ms
5,376 KB
testcase_22 AC 225 ms
5,376 KB
testcase_23 AC 194 ms
5,376 KB
testcase_24 AC 162 ms
5,376 KB
testcase_25 AC 122 ms
5,376 KB
testcase_26 AC 53 ms
5,376 KB
testcase_27 AC 296 ms
5,376 KB
testcase_28 AC 267 ms
5,376 KB
testcase_29 AC 68 ms
5,376 KB
testcase_30 AC 31 ms
5,376 KB
testcase_31 AC 31 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 10000;
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> r(M), c(M);
  for (int i = 0; i < M; i++){
    cin >> r[i] >> c[i];
    r[i]--;
    c[i]--;
  }
  vector<vector<int>> E(N * 2);
  for (int i = 0; i < M; i++){
    E[r[i]].push_back(N + c[i]);
    E[N + c[i]].push_back(r[i]);
  }
  vector<int> a, b;
  int cnt = 0;
  vector<bool> used(N * 2, false);
  for (int i = 0; i < N * 2; i++){
    if (!used[i]){
      used[i] = true;
      int cnta = 0, cntb = 0;
      queue<int> Q;
      Q.push(i);
      while (!Q.empty()){
        int v = Q.front();
        Q.pop();
        if (v < N){
          cnta++;
        }
        if (v >= N){
          cntb++;
        }
        for (int w : E[v]){
          if (!used[w]){
            used[w] = true;
            Q.push(w);
          }
        }
      }
      a.push_back(cnta);
      b.push_back(cntb);
      cnt++;
    }
  }
  vector<int> dp1(N * 2 - 1, -INF);
  vector<int> dp2(N * 2 - 1, INF);
  dp1[0] = 0;
  dp2[0] = 0;
  for (int i = 0; i < cnt; i++){
    for (int j = N * 2 - 2; j >= 0; j--){
      if (j + a[i] + b[i] < N * 2 - 1){
        if (dp1[j] != -INF){
          dp1[j + a[i] + b[i]] = max(dp1[j + a[i] + b[i]], dp1[j] + a[i] - b[i]);
        }
        if (dp2[j] != INF){
          dp2[j + a[i] + b[i]] = min(dp2[j + a[i] + b[i]], dp2[j] + a[i] - b[i]);
        }
      }
    }
  }
  int ans = N * N;
  for (int i = 0; i < N * 2 - 1; i++){
    if (dp1[i] != -INF){
      int sa = (i + dp1[i]) / 2;
      int sb = (i - dp1[i]) / 2;
      ans = min(ans, sa * sb + (N - sa) * (N - sb));
    }
    if (dp2[i] != INF){
      int sa = (i + dp2[i]) / 2;
      int sb = (i - dp2[i]) / 2;
      ans = min(ans, sa * sb + (N - sa) * (N - sb));
    }
  }
  cout << ans - M << endl;
}
0