結果

問題 No.13 囲みたい!
ユーザー 👑 NachiaNachia
提出日時 2020-09-27 13:25:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,009 bytes
コンパイル時間 1,632 ms
コンパイル使用メモリ 170,156 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 01:13:16
合計ジャッジ時間 2,871 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

struct dsu {
    int N;
    vector<int> V;
    dsu(int n) { N = n; V.resize(N); rep(i, N) V[i] = i; }
    int leader(int a) { if (V[a] == a) return a; return V[a] = leader(V[a]); }
    void merge(int a, int b) { V[leader(a)] = leader(b); }
    bool same(int a, int b) { return leader(a) == leader(b); }
};

int W, H;
int M[100][100];

int main() {
    cin >> W >> H;
    rep(y, H) rep(x, W) cin >> M[x][y];
    dsu G(H * W);


    bool possible = false;
    rep(y, H) rep(x, W - 1) if (M[x][y] == M[x + 1][y]) {
        int a = y * W + x, b = a + 1;
        if (G.same(a, b)) possible = true;
        G.merge(a, b);
    }
    rep(y, H - 1) rep(x, W) if (M[x][y] == M[x][y + 1]) {
        int a = y * W + x, b = a + W;
        if (G.same(a, b)) possible = true;
        G.merge(a, b);
    }

    cout << (possible ? "possible" : "impossible") << endl;

    return 0;
}
0