結果

問題 No.1588 Connection
ユーザー Ricky_ponRicky_pon
提出日時 2021-07-08 23:38:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 3,037 bytes
コンパイル時間 2,231 ms
コンパイル使用メモリ 209,148 KB
実行使用メモリ 24,432 KB
平均クエリ数 563.00
最終ジャッジ日時 2023-09-24 11:43:15
合計ジャッジ時間 5,314 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
23,988 KB
testcase_01 AC 23 ms
23,424 KB
testcase_02 AC 24 ms
23,568 KB
testcase_03 AC 25 ms
23,448 KB
testcase_04 AC 24 ms
23,688 KB
testcase_05 AC 24 ms
24,336 KB
testcase_06 AC 26 ms
23,436 KB
testcase_07 AC 23 ms
23,556 KB
testcase_08 AC 25 ms
23,460 KB
testcase_09 AC 26 ms
23,340 KB
testcase_10 AC 27 ms
23,940 KB
testcase_11 AC 26 ms
23,676 KB
testcase_12 AC 56 ms
24,300 KB
testcase_13 AC 64 ms
23,436 KB
testcase_14 AC 23 ms
23,544 KB
testcase_15 AC 25 ms
23,772 KB
testcase_16 AC 24 ms
23,340 KB
testcase_17 AC 23 ms
24,420 KB
testcase_18 AC 23 ms
23,844 KB
testcase_19 AC 24 ms
24,348 KB
testcase_20 AC 26 ms
24,432 KB
testcase_21 AC 105 ms
23,844 KB
testcase_22 AC 107 ms
23,436 KB
testcase_23 AC 58 ms
23,400 KB
testcase_24 AC 43 ms
24,312 KB
testcase_25 AC 67 ms
23,988 KB
testcase_26 AC 67 ms
23,532 KB
testcase_27 AC 42 ms
23,340 KB
testcase_28 AC 37 ms
23,676 KB
testcase_29 AC 108 ms
23,568 KB
testcase_30 AC 111 ms
23,568 KB
testcase_31 AC 24 ms
23,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define For(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rFor(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) For(i, 0, (n))
#define rrep(i, n) rFor(i, (n), 0)
#define fi first
#define se second

using namespace std;

typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct coord_comp {
    vector<T> v;
    bool sorted = false;

    coord_comp() {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        sort(v.begin(), v.end());

        v.erase(unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }

    T &operator[](int i) { return v[i]; }
};

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

typedef struct UnionFindTree {
    vector<int> par;

    UnionFindTree(int n) { par.resize(n, -1); }

    void clear() { fill(par.begin(), par.end(), -1); }

    bool is_root(int x) { return par[x] < 0; }

    int find(int x) {
        if (is_root(x)) return x;
        return par[x] = find(par[x]);
    }

    int size(int x) { return -par[find(x)]; }

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        if (size(x) < size(y)) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }

    bool same(int x, int y) { return find(x) == find(y); }
} UF;

int n, m;
bool used[510][510];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int idx(int x, int y) { return x * n + y; }

string interact(int x, int y) {
    cout << x + 1 << " " << y + 1 << endl;
    string ret;
    cin >> ret;
    return ret;
}

int main() {
    scanf("%d%d", &n, &m);
    UF uf(n * n);
    queue<pii> que;
    que.emplace(0, 0);
    used[0][0] = true;
    while (!que.empty()) {
        auto [x, y] = que.front();
        que.pop();
        rep(i, 4) {
            int nx = x + dx[i], ny = y + dy[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[nx][ny]) {
                if (interact(nx, ny) == "Black") {
                    uf.unite(idx(x, y), idx(nx, ny));
                    que.emplace(nx, ny);
                }
                used[nx][ny] = true;
            }
        }
    }
    cout << (uf.same(idx(0, 0), idx(n - 1, n - 1)) ? "Yes" : "No") << endl;
}
0