結果
問題 | No.1588 Connection |
ユーザー |
|
提出日時 | 2021-07-08 23:42:32 |
言語 | C++17 (gcc 11.2.0 + boost 1.78.0) |
結果 |
AC
|
実行時間 | 102 ms / 2,000 ms |
コード長 | 1,777 bytes |
コンパイル時間 | 2,019 ms |
使用メモリ | 22,864 KB |
平均クエリ数 | 562.31 |
最終ジャッジ日時 | 2023-02-15 01:19:16 |
合計ジャッジ時間 | 5,180 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge11 |
テストケース
テストケース表示入力 | 結果 | 実行時間 使用メモリ |
---|---|---|
testcase_00 | AC | 20 ms
21,940 KB |
testcase_01 | AC | 20 ms
21,880 KB |
testcase_02 | AC | 20 ms
22,204 KB |
testcase_03 | AC | 20 ms
22,600 KB |
testcase_04 | AC | 20 ms
21,880 KB |
testcase_05 | AC | 20 ms
21,916 KB |
testcase_06 | AC | 22 ms
22,864 KB |
testcase_07 | AC | 20 ms
21,892 KB |
testcase_08 | AC | 22 ms
22,600 KB |
testcase_09 | AC | 23 ms
21,892 KB |
testcase_10 | AC | 23 ms
22,432 KB |
testcase_11 | AC | 23 ms
22,192 KB |
testcase_12 | AC | 51 ms
21,892 KB |
testcase_13 | AC | 58 ms
22,216 KB |
testcase_14 | AC | 21 ms
22,204 KB |
testcase_15 | AC | 21 ms
21,980 KB |
testcase_16 | AC | 21 ms
21,892 KB |
testcase_17 | AC | 21 ms
22,204 KB |
testcase_18 | AC | 20 ms
21,928 KB |
testcase_19 | AC | 21 ms
22,680 KB |
testcase_20 | AC | 27 ms
21,928 KB |
testcase_21 | AC | 102 ms
21,768 KB |
testcase_22 | AC | 101 ms
21,892 KB |
testcase_23 | AC | 52 ms
21,880 KB |
testcase_24 | AC | 38 ms
21,756 KB |
testcase_25 | AC | 65 ms
21,756 KB |
testcase_26 | AC | 63 ms
21,940 KB |
testcase_27 | AC | 38 ms
22,420 KB |
testcase_28 | AC | 32 ms
22,588 KB |
testcase_29 | AC | 102 ms
21,892 KB |
testcase_30 | AC | 102 ms
21,940 KB |
testcase_31 | AC | 21 ms
22,204 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) using ll = long long; using P = pair<int,int>; constexpr int dx[4] = {0,1,0,-1}; constexpr int dy[4] = {1,0,-1,0}; class Union_Find{ vector<int> par; vector<int> rank; vector<int> num; public: Union_Find(int n) { par = vector<int>(n); rank = vector<int>(n,0); num = vector<int>(n,1); rep(i,n) par[i] = i; } int find(int x) { if(par[x] == x) return x; else return par[x] = find(par[x]); } int number(int x) { return num[find(x)]; } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) rank[x]++; par[y] = x; num[x] += num[y]; } bool same(int x, int y) { return find(x) == find(y); } }; int main(){ int n, m; cin >> n >> m; vector<vector<int>> c(n,vector<int>(n,-1)); c[0][0] = 1; c[n-1][n-1] = 1; queue<P> que; que.push(P(1,0)); que.push(P(0,1)); while(que.size()) { P p = que.front(); que.pop(); if(c[p.first][p.second] != -1) continue; cout << p.first+1 << " " << p.second+1 << endl; fflush(stdout); string s; cin >> s; c[p.first][p.second] = (s == "Black" ? 1 : 0); if(c[p.first][p.second] == 1) { rep(d,4) { int x = p.first+dx[d], y = p.second+dy[d]; if(x >= 0 and y >= 0 and x < n and y < n and c[x][y] == -1) que.push(P(x,y)); } } } Union_Find uf(n*n); rep(x,n) rep(y,n) { rep(d,4) { int nx = x+dx[d], ny = y+dy[d]; if(nx >= 0 and nx < n and ny >= 0 and ny < n and c[x][y] == 1 and c[nx][ny] == 1) uf.unite(x*n+y,nx*n+ny); } } cout << (uf.same(0,n*n-1) ? "Yes" : "No") << endl; fflush(stdout); return 0; }