結果
| 問題 |
No.2884 Pieces on Squares
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-10 02:51:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 90 ms / 2,000 ms |
| コード長 | 1,721 bytes |
| コンパイル時間 | 2,334 ms |
| コンパイル使用メモリ | 206,476 KB |
| 最終ジャッジ日時 | 2025-02-24 06:28:51 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 45 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
class UnionFind{
public:
vector<int> par,siz;
void make(int N){
par.resize(N,-1);
siz.resize(N,1);
}
int root(int x){
if(par.at(x) == -1) return x;
else return par.at(x) = root(par.at(x));
}
void unite(int u, int v){
u = root(u),v = root(v);
if(u == v) return;
if(siz.at(u) < siz.at(v)) swap(u,v);
par.at(v) = u;
siz.at(u) += siz.at(v);
}
bool issame(int u, int v){
if(root(u) == root(v)) return true;
else return false;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int H,W,N; cin >> H >> W >> N;
UnionFind uf; uf.make(H+W);
while(N--){
int a,b; cin >> a >> b;
a--; b--; b += H;
uf.unite(a,b);
}
vector<pair<int,int>> need(H+W);
for(int i=0; i<H+W; i++){
if(i < H) need.at(uf.root(i)).first++;
else need.at(uf.root(i)).second++;
}
auto update = [&](pair<int,int> &a,pair<int,int> b,int c) -> void {
auto &[mina,maxa] = a;
auto &[minb,maxb] = b;
if(b == make_pair(W*2,-W*2)) return;
mina = min(mina,minb+c);
maxa = max(maxa,maxb+c);
};
vector<pair<int,int>> dp(H+1,{W*2,-W*2});
dp.at(0) = {0,0};
for(auto &[r,c] : need){
if(r+c == 0) continue;
for(int i=H-r; i>=0; i--) update(dp.at(i+r),dp.at(i),c);
}
int answer = 0;
for(int i=0; i<=H; i++){
auto &[minv,maxv] = dp.at(i);
if(minv != W*2) answer = max(answer,(-2*i+H)*minv+i*W);
if(maxv != -W*2) answer = max(answer,(-2*i+H)*maxv+i*W);
}
cout << answer << endl;
}