結果

問題 No.1669 パズル作成
ユーザー SSRSSSRS
提出日時 2021-09-03 22:43:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 341 ms / 2,000 ms
コード長 1,802 bytes
コンパイル時間 1,661 ms
コンパイル使用メモリ 175,804 KB
実行使用メモリ 5,804 KB
最終ジャッジ日時 2023-08-21 23:43:59
合計ジャッジ時間 7,760 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 37 ms
5,140 KB
testcase_07 AC 341 ms
4,376 KB
testcase_08 AC 53 ms
5,804 KB
testcase_09 AC 321 ms
4,380 KB
testcase_10 AC 198 ms
4,376 KB
testcase_11 AC 260 ms
4,376 KB
testcase_12 AC 53 ms
5,712 KB
testcase_13 AC 52 ms
5,620 KB
testcase_14 AC 52 ms
5,688 KB
testcase_15 AC 228 ms
4,380 KB
testcase_16 AC 299 ms
4,380 KB
testcase_17 AC 55 ms
4,380 KB
testcase_18 AC 327 ms
4,380 KB
testcase_19 AC 319 ms
4,376 KB
testcase_20 AC 290 ms
4,376 KB
testcase_21 AC 256 ms
4,380 KB
testcase_22 AC 228 ms
4,380 KB
testcase_23 AC 196 ms
4,376 KB
testcase_24 AC 162 ms
4,380 KB
testcase_25 AC 126 ms
4,376 KB
testcase_26 AC 53 ms
4,376 KB
testcase_27 AC 299 ms
4,380 KB
testcase_28 AC 270 ms
4,376 KB
testcase_29 AC 70 ms
4,380 KB
testcase_30 AC 28 ms
4,632 KB
testcase_31 AC 28 ms
4,692 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