結果

問題 No.1669 パズル作成
ユーザー tko919tko919
提出日時 2021-10-02 16:05:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,792 bytes
コンパイル時間 2,191 ms
コンパイル使用メモリ 206,352 KB
実行使用メモリ 104,536 KB
最終ジャッジ日時 2023-09-27 16:24:33
合計ジャッジ時間 28,380 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,460 KB
testcase_01 AC 2 ms
5,408 KB
testcase_02 AC 2 ms
5,560 KB
testcase_03 AC 8 ms
10,096 KB
testcase_04 AC 6 ms
10,104 KB
testcase_05 AC 3 ms
10,184 KB
testcase_06 AC 33 ms
10,096 KB
testcase_07 RE -
testcase_08 AC 165 ms
101,884 KB
testcase_09 AC 1,016 ms
104,204 KB
testcase_10 AC 1,598 ms
104,280 KB
testcase_11 AC 1,709 ms
104,276 KB
testcase_12 AC 152 ms
101,768 KB
testcase_13 AC 154 ms
101,760 KB
testcase_14 AC 155 ms
101,680 KB
testcase_15 AC 1,741 ms
104,220 KB
testcase_16 AC 979 ms
104,216 KB
testcase_17 AC 411 ms
104,284 KB
testcase_18 AC 831 ms
104,204 KB
testcase_19 AC 1,040 ms
104,344 KB
testcase_20 AC 1,542 ms
104,300 KB
testcase_21 AC 1,782 ms
104,212 KB
testcase_22 AC 1,754 ms
104,228 KB
testcase_23 AC 1,579 ms
104,504 KB
testcase_24 AC 1,236 ms
104,212 KB
testcase_25 AC 953 ms
104,348 KB
testcase_26 AC 414 ms
104,480 KB
testcase_27 AC 1,299 ms
104,228 KB
testcase_28 AC 1,705 ms
104,408 KB
testcase_29 AC 600 ms
104,536 KB
testcase_30 AC 141 ms
102,032 KB
testcase_31 AC 142 ms
101,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

struct UnionFind{
   vector<int> par; int n;
   UnionFind(){}
   UnionFind(int _n):par(_n,-1),n(_n){}
   int root(int x){return par[x]<0?x:par[x]=root(par[x]);}
   bool same(int x,int y){return root(x)==root(y);}
   int size(int x){return -par[root(x)];}
   bool unite(int x,int y){
      x=root(x),y=root(y); if(x==y)return false;
      if(size(x)>size(y))swap(x,y);
      par[y]+=par[x]; par[x]=y; n--; return true;
   }
};

using P=pair<int,int>;
P cnt[10001];
bitset<5001> dp[5001];
int rui[5001][5001];

int main(){
   int n,m;
   cin>>n>>m;
   UnionFind uni(n*2);
   rep(i,0,m){
      int x,y;
      cin>>x>>y;
      x--; y--;
      uni.unite(x,y+n);
   }
   
   int X=0,Y=0;
   rep(i,0,n){
      if(uni.size(i)==1)X++;
      else cnt[uni.root(i)].first++;
   }
   rep(i,n,n*2){
      if(uni.size(i)==1)Y++;
      else cnt[uni.root(i)].second++;
   }

   dp[0][0]=1;
   rep(x,0,n*2)if(cnt[x].first!=0 or cnt[x].second!=0){
      auto [a,b]=cnt[x];
      for(int i=n-a;i>=0;i--)dp[i+a]|=dp[i]<<b;
   }
   rep(a,0,n+1)rep(b,0,n+1)if(dp[a][b]){
      rui[a][b]++;
      rui[a+X+1][b]--;
      rui[a][b+Y+1]--;
      rui[a+X+1][b+Y+1]++;
   }
   rep(i,0,n+1)rep(j,0,n+1)rui[i+1][j]+=rui[i][j];
   rep(i,0,n+1)rep(j,0,n+1)rui[i][j+1]+=rui[i][j];
   int res=inf;
   rep(a,0,n+1)rep(b,0,n+1)if(rui[a][b]){
      chmin(res,a*b+(n-a)*(n-b)-m);
   }
   cout<<res<<'\n';
   return 0;
}
0