結果
| 問題 |
No.1588 Connection
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-07-08 22:49:07 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 108 ms / 2,000 ms |
| コード長 | 2,071 bytes |
| コンパイル時間 | 2,105 ms |
| コンパイル使用メモリ | 183,300 KB |
| 実行使用メモリ | 25,476 KB |
| 平均クエリ数 | 562.59 |
| 最終ジャッジ日時 | 2024-07-17 11:40:46 |
| 合計ジャッジ時間 | 5,252 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 31 |
ソースコード
#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;
}