結果

問題 No.1669 パズル作成
ユーザー SSRSSSRS
提出日時 2021-09-03 21:58:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,396 bytes
コンパイル時間 1,829 ms
コンパイル使用メモリ 178,736 KB
実行使用メモリ 11,204 KB
最終ジャッジ日時 2023-08-21 21:39:12
合計ジャッジ時間 6,091 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,760 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 88 ms
4,376 KB
testcase_04 AC 33 ms
4,376 KB
testcase_05 AC 99 ms
4,380 KB
testcase_06 AC 37 ms
5,172 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
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<vector<bool>> dp(N + 1, vector<bool>(N + 1, false));
  dp[0][0] = true;
  for (int i = 0; i < cnt; i++){
    for (int j = N; j >= 0; j--){
      for (int k = N; k >= 0; k--){
        if (dp[j][k]){
          dp[j + a[i]][k + b[i]] = true;
        }
      }
    }
  }
  int ans = N * N;
  for (int i = 0; i <= N; i++){
    for (int j = 0; j <= N; j++){
      if (dp[i][j]){
        ans = min(ans, i * j + (N - i) * (N - j));
      }
    }
  }
  cout << ans - M << endl;
}
0