結果

問題 No.1669 パズル作成
ユーザー tko919tko919
提出日時 2021-10-02 02:11:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,405 bytes
コンパイル時間 2,572 ms
コンパイル使用メモリ 209,888 KB
実行使用メモリ 11,200 KB
最終ジャッジ日時 2023-09-27 02:28:08
合計ジャッジ時間 7,837 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 29 ms
4,380 KB
testcase_06 AC 35 ms
4,376 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;

#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 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;
   rep(i,0,n){
      cnt[uni.root(i)].first++;
   }
   rep(i,n,n*2){
      cnt[uni.root(i)].second++;
   }

   dp[0][0]=1;
   for(auto& [t,p]:cnt){
      auto [a,b]=p;
      rep(i,0,n-a+1)dp[i+a]|=dp[i]<<b;
   }
   int res=inf;
   rep(a,0,n+1)rep(b,0,n+1)if(dp[a][b]){
      chmin(res,a*b+(n-a)*(n-b)-m);
   }
   cout<<res<<'\n';
   return 0;
}
0