Submission #40873
ソースコード
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 76 77 | #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.tie(0), ios::sync_with_stdio( false ); 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:10:47 |
言語 | C++14 |
状態 | Accepted |
得点 | 10 |
ソースコード長 | 1753 Byte |
最大実行時間 | 142 ms |
最大メモリ使用量 | 16752 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | Easy | 1 / 1 | *easy, *sample* |
2 | ALL | 9 / 9 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # | |
---|---|---|---|---|---|
input-sample1 | AC | 19 ms | 480 KB |
1
|
2
|
input-sample2 | AC | 28 ms | 436 KB |
1
|
2
|
input01-easy | AC | 22 ms | 640 KB |
1
|
2
|
input02-easy | AC | 19 ms | 716 KB |
1
|
2
|
input03-easy | AC | 22 ms | 644 KB |
1
|
2
|
input04 | AC | 125 ms | 5832 KB |
2
|
|
input05 | AC | 82 ms | 5980 KB |
2
|
|
input06 | AC | 109 ms | 6708 KB |
2
|
|
input07 | AC | 125 ms | 7704 KB |
2
|
|
input08 | AC | 125 ms | 10184 KB |
2
|
|
input09 | AC | 130 ms | 11512 KB |
2
|
|
input10 | AC | 142 ms | 12320 KB |
2
|
|
input11 | AC | 129 ms | 16200 KB |
2
|
|
input12 | AC | 114 ms | 16752 KB |
2
|
|
input13 | AC | 20 ms | 11672 KB |
2
|
|
input14 | AC | 23 ms | 11756 KB |
2
|