結果

問題 No.2696 Sign Creation
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-03-22 22:34:41
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 345 ms / 2,500 ms
コード長 3,054 bytes
コンパイル時間 2,988 ms
コンパイル使用メモリ 216,664 KB
実行使用メモリ 10,008 KB
最終ジャッジ日時 2024-12-20 12:11:07
合計ジャッジ時間 7,434 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct UnionFind {
int ngroup, N;
vector<int> par, siz;
UnionFind(int _N) : N(_N), par(_N), siz(_N){
ngroup = _N;
for(int i = 0; i < N; i++) par[i] = i, siz[i] = 1;
}
int root(int x) {
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
int unite(int x, int y) {
int rx = root(x), ry = root(y);
if (rx == ry) return rx;
ngroup--;
if (siz[rx] > siz[ry]) swap(rx, ry);
par[rx] = ry; siz[ry] += siz[rx];
return ry;
}
bool same(int x, int y) {
int rx = root(x), ry = root(y);
return rx == ry;
}
int size(int x) {return siz[root(x)];}
int group_count() {return ngroup;}
vector<vector<int>> groups(){
vector<int> rev(N); int nrt=0;
for (int i=0; i<N; i++) if (root(i) == i) rev[i] = nrt, nrt++;
vector<vector<int>> res(nrt);
for (int i=0; i<N; i++) res[rev[root(i)]].push_back(i);
return res;
}
};
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
ll H, W, N, D, cnt=0, mi, mx, c, d;
cin >> H >> W >> N >> D;
vector<ll> x(N), y(N);
unordered_set<ll> st;
auto check=[&](ll h, ll w){
return 0<=h && h<H && 0<=w && w<W;
};
auto f=[&](ll h, ll w){
return h*W+w;
};
for (int i=0; i<N; i++){
cin >> x[i] >> y[i];
x[i]--; y[i]--;
st.insert(f(x[i], y[i]));
}
UnionFind uf(H*W);
for (int i=0; i<N; i++){
int h, w;
for (int j=-5; j<=5; j++){
for (int k=-5; k<=5; k++){
h=x[i]+j, w=y[i]+k;
if (!check(h, w) || abs(j)+abs(k) > D) continue;
if (st.count(f(h, w))) uf.unite(f(x[i], y[i]), f(h, w));
}
}
}
for (int i=0; i<H; i++){
for (int j=0; j<W; j++){
if (st.count(f(i, j)) && uf.root(f(i, j)) == f(i, j)){
if (uf.size(f(i, j)) > 1) cnt++;
}
}
}
mi = cnt;
mx = cnt;
for (int i=0; i<H; i++){
for (int j=0; j<W; j++){
if (st.count(f(i, j))) continue;
unordered_set<ll> st2;
c = 0;
for (int k=-5; k<=5; k++){
for (int l=-5; l<=5; l++){
int h=i+k, w=j+l;
if (!check(h, w) || abs(k)+abs(l) > D) continue;
if (st.count(f(h, w))){
if (uf.size(f(h, w)) == 1) c = 1;
if (uf.size(f(h, w)) > 1) st2.insert(uf.root(f(h, w)));
}
}
}
d = st2.size();
if (d == 0 && c == 1){
mi = min(mi, cnt+1);
mx = max(mx, cnt+1);
}
else if (d >= 1){
mi = min(mi, cnt-(d-1));
mx = max(mx, cnt-(d-1));
}
}
}
cout << mi << " " << mx << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0