結果

問題 No.1669 パズル作成
ユーザー tko919tko919
提出日時 2021-10-02 16:00:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,813 ms / 2,000 ms
コード長 1,760 bytes
コンパイル時間 2,052 ms
コンパイル使用メモリ 212,792 KB
実行使用メモリ 104,904 KB
最終ジャッジ日時 2023-09-27 16:20:34
合計ジャッジ時間 27,922 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,428 KB
testcase_01 AC 2 ms
5,460 KB
testcase_02 AC 1 ms
5,640 KB
testcase_03 AC 8 ms
10,256 KB
testcase_04 AC 6 ms
10,048 KB
testcase_05 AC 3 ms
10,084 KB
testcase_06 AC 33 ms
10,048 KB
testcase_07 AC 102 ms
101,688 KB
testcase_08 AC 151 ms
101,756 KB
testcase_09 AC 1,014 ms
104,656 KB
testcase_10 AC 1,574 ms
104,824 KB
testcase_11 AC 1,734 ms
104,632 KB
testcase_12 AC 156 ms
101,980 KB
testcase_13 AC 152 ms
101,748 KB
testcase_14 AC 149 ms
101,756 KB
testcase_15 AC 1,802 ms
104,904 KB
testcase_16 AC 1,001 ms
104,516 KB
testcase_17 AC 410 ms
104,756 KB
testcase_18 AC 860 ms
104,584 KB
testcase_19 AC 1,082 ms
104,568 KB
testcase_20 AC 1,559 ms
104,696 KB
testcase_21 AC 1,802 ms
104,672 KB
testcase_22 AC 1,813 ms
104,640 KB
testcase_23 AC 1,609 ms
104,692 KB
testcase_24 AC 1,261 ms
104,680 KB
testcase_25 AC 968 ms
104,632 KB
testcase_26 AC 422 ms
104,560 KB
testcase_27 AC 1,334 ms
104,660 KB
testcase_28 AC 1,674 ms
104,672 KB
testcase_29 AC 565 ms
104,640 KB
testcase_30 AC 129 ms
101,696 KB
testcase_31 AC 129 ms
101,692 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;
   }
};

bitset<5010> dp[5010];
int rui[5010][5010];

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);
   }
   using P=pair<int,int>;
   map<int,P> cnt;
   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;
   for(auto& [t,p]:cnt){
      auto [a,b]=p;
      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