結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
not_522
|
| 提出日時 | 2015-08-18 20:32:14 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 5,000 ms |
| コード長 | 1,158 bytes |
| コンパイル時間 | 1,633 ms |
| コンパイル使用メモリ | 165,468 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-13 10:46:55 |
| 合計ジャッジ時間 | 1,927 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
#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;
}
not_522