結果

問題 No.179 塗り分け
ユーザー h-izuh-izu
提出日時 2022-05-15 20:20:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,228 ms / 3,000 ms
コード長 6,386 bytes
コンパイル時間 4,675 ms
コンパイル使用メモリ 280,924 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-10-11 07:39:18
合計ジャッジ時間 26,021 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 6 ms
4,368 KB
testcase_03 AC 5 ms
4,368 KB
testcase_04 AC 7 ms
4,372 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 128 ms
4,368 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 38 ms
4,372 KB
testcase_09 AC 37 ms
4,500 KB
testcase_10 AC 1,405 ms
4,368 KB
testcase_11 AC 1,213 ms
4,368 KB
testcase_12 AC 2,228 ms
4,368 KB
testcase_13 AC 5 ms
4,368 KB
testcase_14 AC 7 ms
4,372 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 2 ms
4,368 KB
testcase_18 AC 641 ms
4,368 KB
testcase_19 AC 597 ms
4,368 KB
testcase_20 AC 1 ms
4,372 KB
testcase_21 AC 1 ms
4,368 KB
testcase_22 AC 224 ms
4,372 KB
testcase_23 AC 353 ms
4,372 KB
testcase_24 AC 159 ms
4,368 KB
testcase_25 AC 91 ms
4,368 KB
testcase_26 AC 268 ms
4,372 KB
testcase_27 AC 837 ms
4,368 KB
testcase_28 AC 650 ms
4,368 KB
testcase_29 AC 1,311 ms
4,368 KB
testcase_30 AC 1,052 ms
4,368 KB
testcase_31 AC 1,613 ms
4,372 KB
testcase_32 AC 1,013 ms
4,368 KB
testcase_33 AC 1,573 ms
4,372 KB
testcase_34 AC 1,130 ms
4,372 KB
testcase_35 AC 1,877 ms
4,372 KB
testcase_36 AC 3 ms
4,372 KB
testcase_37 AC 1 ms
4,372 KB
testcase_38 AC 6 ms
4,368 KB
testcase_39 AC 5 ms
4,372 KB
testcase_40 AC 7 ms
4,372 KB
testcase_41 AC 1 ms
4,368 KB
testcase_42 AC 9 ms
4,372 KB
testcase_43 AC 126 ms
4,372 KB
testcase_44 AC 380 ms
4,372 KB
testcase_45 AC 39 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>

#include <atcoder/all>
#include <vector>
using namespace std;
using namespace atcoder;

typedef unsigned long long ull;
typedef long long ll;

const double PI = 3.14159265358979323846;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

// a^p
// 2^3 = 2 * 2^2
// 2^2 = 2 * (2^1)
// 2^1 = 2
ll modpow(ll a, ll p, ll mod) {
  if (p == 0) return 1;

  if (p % 2 == 0) {
    ll half = modpow(a, p / 2, mod) % mod;
    return half * half % mod;
  } else {
    return a * modpow(a, p - 1, mod) % mod;
  }
}

// a^p
ll powpow(ll a, ll p) {
  if (p == 0) return 1;

  if (p % 2 == 0) {
    ll half = pow(a, p / 2);
    return half * half;
  } else {
    return a * pow(a, p - 1);
  }
}

// a/b
// https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a
ll moddiv(ll a, ll b, ll mod) { return a * modpow(b, mod - 2, mod); }

// nCa を求める
ll modCombination(ll n, ll a, ll mod) {
  if (n < 0 || a < 0 || n < a) return 0;
  if (n - a < a) {
    return modCombination(n, n - a, mod);
  }

  ll denominator = 1;  // 分母
  ll numerator = 1;    // 分子

  for (ll i = 0; i < a; i++) {
    denominator *= a - i;
    numerator *= n - i;
    denominator %= mod;
    numerator %= mod;
  }

  return numerator * modpow(denominator, mod - 2, mod) % mod;
}

vector<vector<ll>> combination(ll n) {
  vector<vector<ll>> C(n + 1, vector<ll>(n + 1));
  C[0][0] = 1;
  rep(i, n) rep(j, i + 1) {
    C[i + 1][j + 1] += C[i][j];
    C[i + 1][j] += C[i][j];
  }

  return C;
}

// ref. https://drken1215.hatenablog.com/entry/2018/06/08/210000
class ModCombinationTale {
 private:
  ll n;
  ll mod;
  vector<ll> fac, finv, inv;

 public:
  ModCombinationTale(ll n, ll mod) : n(n), mod(mod) {
    fac.resize(n + 1);
    finv.resize(n + 1);
    inv.resize(n + 1);

    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;

    for (ll i = 2; i <= n; i++) {
      fac[i] = fac[i - 1] * i % mod;
      inv[i] = mod - inv[mod % i] * (mod / i) % mod;
      finv[i] = finv[i - 1] * inv[i] % mod;
    }
  }

  ll operator()(ll n, ll k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
  }
};

class UnionFind {
 private:
  vector<ll> parents;

 public:
  UnionFind(ll n) : parents(n, -1) {}

  bool issame(ll x, ll y) { return root(x) == root(y); }

  bool merge(ll x, ll y) {
    if (issame(x, y)) return false;

    ll rx = root(x);
    ll ry = root(y);
    if (parents[rx] > parents[ry]) swap(rx, ry);
    // サイズ情報を更新
    parents[rx] += parents[ry];
    // yの親を更新
    parents[ry] = rx;

    return true;
  }

  ll size(ll x) { return -parents[root(x)]; }

  ll root(ll x) {
    if (parents[x] < 0) return x;
    // 根の親の値に木の(-)サイズの情報を入れる
    return parents[x] = root(parents[x]);
  }
};

// cf. https://qiita.com/drken/items/a14e9af0ca2d857dad23
vector<ll> enum_divisors(ll n) {
  vector<ll> res;
  // sqrt(n)まで試し割り
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      // 重複しないならばiの相方であるn/iも約数
      // e.g. n=25のときのi=5は重複
      if (n / i != i) res.push_back(n / i);
    }
  }

  sort(res.begin(), res.end());
  return res;
}

// cf. https://qiita.com/drken/items/a14e9af0ca2d857dad23
map<ll, ll> prime_factors(ll n) {
  map<ll, ll> res;
  // sqrt(n)まで試し割り
  for (ll a = 2; a * a <= n; a++) {
    if (n % a != 0) continue;

    // nで割れる限り割る
    while (n % a == 0) {
      res[a]++;
      n /= a;
    }
  }
  if (n != 1) res[n]++;

  return res;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

// cf. https://qiita.com/drken/items/56a6b68edef8fc605821
class AccumSum2D {
 private:
  vector<vector<ll>> sum;
  ll H;
  ll W;

 public:
  AccumSum2D(vector<vector<ll>> &A) {
    H = (ll)A.size();
    W = (ll)A[0].size();
    sum.resize(H + 1, vector<ll>(W + 1));

    for (ll i = 0; i < H; i++) {
      for (ll j = 0; j < W; j++) {
        sum[i + 1][j + 1] = sum[i][j + 1] + sum[i + 1][j] - sum[i][j] + A[i][j];
      }
    }
  }

  // クエリ [x1, x2) × [y1, y2) の長方形区域の和
  ll Sum(ll x1, ll x2, ll y1, ll y2) {
    return sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1];
  }
};

// p/q
struct fraction {
  ll p, q;
  fraction(ll _p = 0, ll _q = 1) : p(_p), q(_q) {
    if (q == 0) {
      p = 1;
      return;
    }

    ll g = gcd(p, q);
    p /= g;
    q /= g;
  }

  bool operator<(const fraction &other) const {
    return p * other.q < q * other.p;
  }

  bool operator<=(const fraction &other) const {
    return p * other.q <= q * other.p;
  }

  bool operator==(const fraction &other) const {
    return p == other.p && q == other.q;
  }
};

// res[i][c] := i 文字目以降で最初に文字 c が登場する index (存在しないときは n)
vector<vector<ll>> calcNext(const string &S) {
  ll n = (ll)S.size();
  vector<vector<ll>> res(n + 1, vector<ll>(26, n));
  for (ll i = n - 1; i >= 0; --i) {
    for (ll j = 0; j < 26; ++j) res[i][j] = res[i + 1][j];
    res[i][S[i] - 'a'] = i;
  }
  return res;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll H, W;
  cin >> H >> W;
  vector<string> G(H);
  rep(i, H) cin >> G[i];

  ll cnt = 0;
  rep(i, H) rep(j, W) {
    if (G[i][j] == '#') cnt++;
  }

  if (cnt % 2 != 0) {
    cout << "NO" << endl;
    return 0;
  }

  if (cnt == 0) {
    cout << "NO" << endl;
    return 0;
  }

  for (ll dx = -50; dx <= 50; dx++) {
    for (ll dy = -50; dy <= 50; dy++) {
      ll matched = 0;
      set<pair<ll, ll>> st;
      rep(i, H) rep(j, W) {
        if (G[i][j] != '#') continue;
        ll ni = i - dx;
        ll nj = j - dy;
        if (ni < 0 || nj < 0) {
          st.emplace(i, j);
          continue;
        }
        if (ni >= H || nj >= W) {
          st.emplace(i, j);
          continue;
        }
        if (G[ni][nj] == '#' && st.count(make_pair(ni, nj))) {
          matched++;
        } else {
          st.emplace(i, j);
        }
      }

      if (matched == cnt / 2) {
        cout << "YES" << endl;
        // cout << dx << " " << dy << endl;
        return 0;
      }
    }
  }

  cout << "NO" << endl;

  return 0;
}
0