結果
| 問題 | 
                            No.1669 パズル作成
                             | 
                    
| コンテスト | |
| ユーザー | 
                             tko919
                         | 
                    
| 提出日時 | 2021-10-02 02:14:37 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,414 bytes | 
| コンパイル時間 | 2,411 ms | 
| コンパイル使用メモリ | 205,620 KB | 
| 最終ジャッジ日時 | 2025-01-24 19:53:42 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 TLE * 17 | 
ソースコード
#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;
      for(int i=n-a;i>=0;i--)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;
}
            
            
            
        
            
tko919