結果

問題 No.13 囲みたい!
ユーザー 👑 jupirojupiro
提出日時 2020-07-15 05:18:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 3,004 bytes
コンパイル時間 2,735 ms
コンパイル使用メモリ 145,736 KB
実行使用メモリ 5,956 KB
最終ジャッジ日時 2023-08-13 02:17:36
合計ジャッジ時間 3,427 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,632 KB
testcase_04 AC 7 ms
5,956 KB
testcase_05 AC 5 ms
4,460 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 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 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;


void dfs_tree(int cur, int pre, vector<bool>& used, vector<pair<int, int>>& rest, vector<vector<int>>& g, vector<vector<int>>& tree) {
    used[cur] = true;
    for (auto next : g[cur]) {
        if (next == pre) continue;
        if (used[next]) {
            rest.emplace_back(cur, next);
        }
        else {
            tree[cur].emplace_back(next);
            tree[next].emplace_back(cur);
            dfs_tree(next, cur, used, rest, g, tree);
        }
    }
}

int dh[] = { 1,-1,0,0 };
int dw[] = { 0,0,1,-1 };

int main()
{
    /*
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    */
    int H, W; scanf("%d %d", &W, &H);
    vector<vector<int>> a(H, vector<int>(W)); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) scanf("%d", &a[i][j]);
    vector<vector<bool>> used(H, vector<bool>(W));
    vector<bool> f(H * W);
    vector<vector<int>> g(H * W);
    vector<vector<int>> tree(H * W);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (used[i][j]) continue;
            queue<pair<int, int>> q;
            used[i][j] = true;
            q.emplace(i, j);
            while (!q.empty()) {
                auto [h, w] = q.front();
                q.pop();
                for (int i = 0; i < 4; i++) {
                    int nh = h + dh[i];
                    int nw = w + dw[i];
                    if (nh < 0 or nh >= H or nw < 0 or nw >= W) continue;
                    if (a[nh][nw] != a[h][w]) continue;
                    if (h < nh or w < nw) g[h * W + w].emplace_back(nh * W + nw), g[nh * W + nw].emplace_back(h * W + w);
                    if (used[nh][nw]) continue;
                    used[nh][nw] = true;
                    q.emplace(nh, nw);
                }
            }
            vector<pair<int, int>> rest;
            dfs_tree(i * W + j, -1, f, rest, g, tree);
            if (!rest.empty()) {
                puts("possible");
                return 0;
            }
        }
    }
    puts("impossible");
    return 0;
}
0