結果
問題 | No.1588 Connection |
ユーザー | kens |
提出日時 | 2021-07-08 23:42:32 |
言語 | C++17 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 110 ms / 2,000 ms |
コード長 | 1,777 bytes |
コンパイル時間 | 2,487 ms |
コンパイル使用メモリ | 210,616 KB |
実行使用メモリ | 24,324 KB |
平均クエリ数 | 562.31 |
最終ジャッジ日時 | 2023-09-24 11:44:30 |
合計ジャッジ時間 | 5,840 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 29 ms
23,304 KB |
testcase_01 | AC | 24 ms
24,312 KB |
testcase_02 | AC | 25 ms
23,520 KB |
testcase_03 | AC | 25 ms
23,580 KB |
testcase_04 | AC | 24 ms
23,532 KB |
testcase_05 | AC | 24 ms
23,340 KB |
testcase_06 | AC | 24 ms
23,292 KB |
testcase_07 | AC | 23 ms
23,556 KB |
testcase_08 | AC | 25 ms
23,712 KB |
testcase_09 | AC | 27 ms
23,904 KB |
testcase_10 | AC | 27 ms
23,580 KB |
testcase_11 | AC | 28 ms
23,544 KB |
testcase_12 | AC | 58 ms
24,288 KB |
testcase_13 | AC | 66 ms
23,340 KB |
testcase_14 | AC | 24 ms
23,544 KB |
testcase_15 | AC | 27 ms
23,712 KB |
testcase_16 | AC | 24 ms
24,108 KB |
testcase_17 | AC | 25 ms
23,544 KB |
testcase_18 | AC | 26 ms
23,328 KB |
testcase_19 | AC | 27 ms
23,472 KB |
testcase_20 | AC | 32 ms
23,928 KB |
testcase_21 | AC | 110 ms
23,712 KB |
testcase_22 | AC | 109 ms
23,556 KB |
testcase_23 | AC | 58 ms
23,460 KB |
testcase_24 | AC | 44 ms
23,556 KB |
testcase_25 | AC | 73 ms
24,204 KB |
testcase_26 | AC | 73 ms
23,424 KB |
testcase_27 | AC | 42 ms
23,472 KB |
testcase_28 | AC | 37 ms
24,264 KB |
testcase_29 | AC | 105 ms
24,324 KB |
testcase_30 | AC | 109 ms
23,460 KB |
testcase_31 | AC | 24 ms
23,556 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; }