結果
| 問題 |
No.1669 パズル作成
|
| コンテスト | |
| ユーザー |
Rubikun
|
| 提出日時 | 2024-11-24 20:47:00 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 454 ms / 2,000 ms |
| コード長 | 3,250 bytes |
| コンパイル時間 | 2,494 ms |
| コンパイル使用メモリ | 204,432 KB |
| 最終ジャッジ日時 | 2025-02-25 06:09:38 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=5005,INF=15<<26;
int mi[MAX*2][MAX],ma[MAX*2][MAX];
struct UF{
int n;
vector<int> par,size,edge,X,Y;
void init(int n_){
n=n_;
par.assign(n,-1);
size.assign(n,1);
edge.assign(n,0);
X.assign(n,0);
Y.assign(n,0);
for(int i=0;i<n;i++){
par[i]=i;
if(i<n/2) X[i]=1;
else Y[i]=1;
}
}
int root(int a){
if(par[a]==a) return a;
else return par[a]=root(par[a]);
}
void unite(int a,int b){
edge[root(a)]++;
if(root(a)!=root(b)){
size[root(a)]+=size[root(b)];
edge[root(a)]+=edge[root(b)];
X[root(a)]+=X[root(b)];
Y[root(a)]+=Y[root(b)];
par[root(b)]=root(a);
}
}
bool check(int a,int b){
return root(a)==root(b);
}
};
int main(){
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
int N,M;cin>>N>>M;
UF uf;uf.init(2*N);
for(int i=0;i<M;i++){
int a,b;cin>>a>>b;a--;b--;
uf.unite(a,N+b);
}
vii S;
for(int i=0;i<2*N;i++){
if(uf.root(i)==i){
int a=uf.X[i],b=uf.Y[i];
//cout<<a<<" "<<b<<endl;
S.pb(mp(a,b));
}
}
for(int i=0;i<=si(S);i++){
for(int j=0;j<=N;j++){
mi[i][j]=INF;
ma[i][j]=-INF;
}
}
mi[0][0]=ma[0][0]=0;
for(int i=0;i<si(S);i++){
auto [a,b]=S[i];
for(int j=0;j<=N;j++){
//cout<<i<<" "<<j<<" "<<mi[i][j]<<" "<<ma[i][j]<<endl;
if(mi[i][j]<=N){
chmin(mi[i+1][j],mi[i][j]);
chmax(ma[i+1][j],ma[i][j]);
}
if(j+a<=N&&ma[i][j]>=0){
chmin(mi[i+1][j+a],ma[i][j]+b);
chmax(ma[i+1][j+a],ma[i][j]+b);
}
}
}
int ans=INF;
for(int a=0;a<=N;a++){
//cout<<a<<" "<<mi[si(S)][a]<<" "<<ma[si(S)][a]<<endl;
{
int b=mi[si(S)][a];
if(b!=INF){
chmin(ans,-(a+b)*N+2*a*b+N*N);
}
}
{
int b=ma[si(S)][a];
if(b!=-INF){
chmin(ans,-(a+b)*N+2*a*b+N*N);
}
}
}
ans-=M;
cout<<ans<<endl;
}
Rubikun