結果

問題 No.13 囲みたい!
ユーザー not_522not_522
提出日時 2015-08-18 20:32:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,158 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 165,492 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:21:08
合計ジャッジ時間 2,633 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

class UnionFind {
private:
  int n;
  vector<int> a;
public:
  UnionFind(int n) : n(n), a(n, -1) {}
  int find(int x) {return a[x] < 0 ? x : (a[x] = find(a[x]));}
  bool equal(int x, int y) {return find(x) == find(y);}
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return;
    if (a[x] > a[y]) swap(x, y);
    a[x] += a[y];
    a[y] = x;
    --n;
  }
  int size() {return n;}
  int size(int x) {return -a[find(x)];}
};

int main() {
  int w, h;
  cin >> w >> h;
  vector<vector<int>> m(h, vector<int>(w));
  for (auto& i : m) {
    for (int& j : i) cin >> j;
  }
  UnionFind uf(w * h);
  bool possible = false;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (i != h - 1 && m[i][j] == m[i + 1][j]) {
        if (uf.equal(i * w + j, i * w + j + w)) possible = true;
        uf.unite(i * w + j, i * w + j + w);
      }
      if (j != w - 1 && m[i][j] == m[i][j + 1]) {
        if (uf.equal(i * w + j, i * w + j + 1)) possible = true;
        uf.unite(i * w + j, i * w + j + 1);
      }
    }
  }
  cout << (possible ? "possible" : "impossible") << endl;
}
0