Submission #40874
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <iostream> #include <vector> #include <algorithm> using namespace std; #define rep(i,n) for(int i=0; i<n; ++i) struct UnionFind { vector< int > uni; explicit UnionFind( int n): uni(n+5, -1) {} int root( int x) { return uni[x]<0 ? x : uni[x]=root(uni[x]); } void unite( int x, int y) { x=root(x), y=root(y); if (x == y) return ; if (uni[y] < uni[x]) swap(x, y); uni[x] += uni[y]; uni[y] = x; } int size( int i) { return -uni[root(i)]; } }; constexpr int dx[] = {1, 0, -1, 0}; constexpr int dy[] = {0, 1, 0, -1}; int H, W, Q; char mas[1005][1005]; #define index1D(x, y) (W*(y) + (x)) int main() { cin >> H >> W >> Q; rep(i, H) cin >> mas[i]; UnionFind uf((H+1) * (W+1)); rep(i, H) { rep(j, W) { if (mas[i][j] == '#' ) { const int idx = index1D(j, i); if (mas[i][j+1] == '#' ) uf.unite(idx, index1D(j+1, i)); if (mas[i+1][j] == '#' ) uf.unite(idx, index1D(j, i+1)); } } } while (Q--) { int op, y, x; cin >> op >> y >> x; --y, --x; const int idx = index1D(x, y); if (op == 0) { mas[y][x] = '#' ; rep(i, 4) { const int nx = x+dx[i]; const int ny = y+dy[i]; if (nx<0 || ny<0 || mas[ny][nx] != '#' ) continue ; uf.unite(idx, index1D(nx, ny)); } } else { cout << ((mas[y][x] == '#' )? uf.size(idx) : 0) << '\n' ; } } return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 0971 - 島の創造 |
ユーザー名 | Arumakan_ei1727 |
投稿日時 | 2018-08-09 00:20:51 |
言語 | C++14 |
状態 | Accepted |
得点 | 10 |
ソースコード長 | 1705 Byte |
最大実行時間 | 1357 ms |
最大メモリ使用量 | 16780 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | Easy | 1 / 1 | *easy, *sample* |
2 | ALL | 9 / 9 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # | |
---|---|---|---|---|---|
input-sample1 | AC | 28 ms | 480 KB |
1
|
2
|
input-sample2 | AC | 20 ms | 448 KB |
1
|
2
|
input01-easy | AC | 25 ms | 536 KB |
1
|
2
|
input02-easy | AC | 38 ms | 628 KB |
1
|
2
|
input03-easy | AC | 28 ms | 708 KB |
1
|
2
|
input04 | AC | 633 ms | 5796 KB |
2
|
|
input05 | AC | 407 ms | 6092 KB |
2
|
|
input06 | AC | 601 ms | 6724 KB |
2
|
|
input07 | AC | 710 ms | 7608 KB |
2
|
|
input08 | AC | 755 ms | 10104 KB |
2
|
|
input09 | AC | 750 ms | 11576 KB |
2
|
|
input10 | AC | 768 ms | 12288 KB |
2
|
|
input11 | AC | 1357 ms | 16196 KB |
2
|
|
input12 | AC | 736 ms | 16780 KB |
2
|
|
input13 | AC | 24 ms | 11852 KB |
2
|
|
input14 | AC | 23 ms | 11824 KB |
2
|