結果

問題 No.1669 パズル作成
ユーザー tko919tko919
提出日時 2021-10-02 16:06:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,752 ms / 2,000 ms
コード長 1,792 bytes
コンパイル時間 2,087 ms
コンパイル使用メモリ 207,520 KB
実行使用メモリ 104,508 KB
最終ジャッジ日時 2023-09-27 16:25:29
合計ジャッジ時間 26,802 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,348 KB
testcase_01 AC 1 ms
5,372 KB
testcase_02 AC 1 ms
5,352 KB
testcase_03 AC 7 ms
10,032 KB
testcase_04 AC 6 ms
10,180 KB
testcase_05 AC 2 ms
10,280 KB
testcase_06 AC 31 ms
10,000 KB
testcase_07 AC 107 ms
101,608 KB
testcase_08 AC 162 ms
101,760 KB
testcase_09 AC 1,041 ms
104,320 KB
testcase_10 AC 1,538 ms
104,352 KB
testcase_11 AC 1,707 ms
104,508 KB
testcase_12 AC 163 ms
101,900 KB
testcase_13 AC 162 ms
101,704 KB
testcase_14 AC 162 ms
101,752 KB
testcase_15 AC 1,752 ms
104,336 KB
testcase_16 AC 990 ms
104,112 KB
testcase_17 AC 399 ms
104,172 KB
testcase_18 AC 840 ms
104,408 KB
testcase_19 AC 1,002 ms
104,296 KB
testcase_20 AC 1,568 ms
104,400 KB
testcase_21 AC 1,746 ms
104,340 KB
testcase_22 AC 1,745 ms
104,296 KB
testcase_23 AC 1,559 ms
104,324 KB
testcase_24 AC 1,222 ms
104,180 KB
testcase_25 AC 943 ms
104,268 KB
testcase_26 AC 403 ms
104,480 KB
testcase_27 AC 1,298 ms
104,432 KB
testcase_28 AC 1,594 ms
104,488 KB
testcase_29 AC 554 ms
104,268 KB
testcase_30 AC 131 ms
101,724 KB
testcase_31 AC 127 ms
101,720 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[10002];
bitset<5002> dp[5002];
int rui[5002][5002];

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