結果

問題 No.1588 Connection
ユーザー nawawannawawan
提出日時 2021-07-08 22:49:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 2,071 bytes
コンパイル時間 1,778 ms
コンパイル使用メモリ 180,028 KB
実行使用メモリ 24,384 KB
平均クエリ数 562.59
最終ジャッジ日時 2023-09-24 10:49:06
合計ジャッジ時間 5,007 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
24,048 KB
testcase_01 AC 25 ms
23,544 KB
testcase_02 AC 24 ms
23,820 KB
testcase_03 AC 24 ms
24,384 KB
testcase_04 AC 27 ms
23,664 KB
testcase_05 AC 27 ms
23,424 KB
testcase_06 AC 28 ms
24,036 KB
testcase_07 AC 27 ms
23,532 KB
testcase_08 AC 29 ms
23,544 KB
testcase_09 AC 30 ms
24,228 KB
testcase_10 AC 28 ms
23,544 KB
testcase_11 AC 29 ms
23,844 KB
testcase_12 AC 57 ms
24,036 KB
testcase_13 AC 65 ms
23,628 KB
testcase_14 AC 24 ms
23,424 KB
testcase_15 AC 25 ms
23,544 KB
testcase_16 AC 24 ms
23,400 KB
testcase_17 AC 25 ms
23,976 KB
testcase_18 AC 25 ms
24,228 KB
testcase_19 AC 25 ms
24,336 KB
testcase_20 AC 28 ms
23,400 KB
testcase_21 AC 109 ms
23,640 KB
testcase_22 AC 108 ms
24,336 KB
testcase_23 AC 59 ms
24,336 KB
testcase_24 AC 43 ms
23,436 KB
testcase_25 AC 69 ms
24,060 KB
testcase_26 AC 67 ms
24,276 KB
testcase_27 AC 42 ms
24,036 KB
testcase_28 AC 38 ms
23,856 KB
testcase_29 AC 112 ms
24,348 KB
testcase_30 AC 109 ms
23,664 KB
testcase_31 AC 24 ms
23,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int out(int a, int b, vector<vector<int>>&F){
    if(F[a - 1][b - 1] != -1) return F[a - 1][b - 1];
    cout << a << ' ' << b << endl;
    string S;
    cin >> S;
    if(S == "Black") return 1;
    else if(S == "White") return 0;
    else return -1;
}
struct UnionFind{
    vector<int> par;
    vector<int> rank;
    UnionFind(int n){
        par.resize(n);
        rank.resize(n);
        for(int i = 0; i < n; i++){
            par[i] = i;
            rank[i] = 1;
        }
    }

    int root(int x){
        if(par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

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

    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if(x == y) return;
        if(rank[x] < rank[y]) swap(x, y);
        par[y] = x;
        rank[x] += rank[y];
    }

    int size(int x) {
        return rank[root(x)];
    }
};
int main(){
    int N, M;
    cin >> N >> M;
    vector<vector<int>> F(N, vector<int>(N, -1));
    F[0][0] = 1;
    F[N - 1][N - 1] = 1;
    UnionFind U(N * N);
    queue<int> q;
    q.push(0);
    vector<vector<bool>> used(N, vector<bool>(N, false));
    used[0][0] = true;
    int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
    while(!q.empty()){
        int now = q.front();
        q.pop();
        int x = now / N, y = now % N;
        for(int i = 0; i < 4; i++){
            int nx = x + dx[i], ny = y + dy[i];
            if(0 <= nx && nx < N && 0 <= ny && ny < N){
                int color = out(nx + 1, ny + 1, F);
                F[nx][ny] = color;
                if(color == -1){
                    return 0;
                }
                if(color == 1) {
                    U.unite(now, nx * N + ny);
                    if(!used[nx][ny]){
                        q.push(nx * N + ny);
                        used[nx][ny] = true;
                    }
                }
            }
        }
    }
    if(!U.same(0, N * N - 1)) cout << "No" << endl;
    else cout << "Yes" << endl;
}
0