結果
問題 |
No.307 最近色塗る問題多くない?
|
ユーザー |
|
提出日時 | 2016-03-01 21:46:35 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,187 bytes |
コンパイル時間 | 1,111 ms |
コンパイル使用メモリ | 76,332 KB |
実行使用メモリ | 23,328 KB |
最終ジャッジ日時 | 2024-09-24 13:12:47 |
合計ジャッジ時間 | 10,166 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 WA * 15 TLE * 1 -- * 10 |
ソースコード
#include <iostream> #include <vector> #include <set> #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) typedef long long ll; using namespace std; struct union_find { vector<int> tree; explicit union_find(size_t n) : tree(n, -1) {} bool is_root(int a) { return tree[a] < 0; } int find_root(int a) { return is_root(a) ? a : (tree[a] = find_root(tree[a])); } int tree_size(int a) { return - tree[find_root(a)]; } void union_tree(int a, int b) { a = find_root(a); b = find_root(b); if (a != b) { if (not (tree_size(a) < tree_size(b))) swap(a,b); tree[b] += tree[a]; tree[a] = b; } } bool is_connected(int a, int b) { return find_root(a) == find_root(b); } }; int main() { int h, w; cin >> h >> w; auto ix = [&](int y, int x) { return y*w + x; }; vector<int> a(h * w); repeat (y,h) repeat (x,w) cin >> a[ix(y,x)]; union_find t(h * w); repeat (y,h) repeat (x,w) { if (y+1 < h and a[ix(y,x)] == a[ix(y+1,x)]) t.union_tree(ix(y,x), ix(y+1,x)); if (x+1 < w and a[ix(y,x)] == a[ix(y,x+1)]) t.union_tree(ix(y,x), ix(y,x+1)); } vector<set<int> > g(h * w); repeat (y,h) repeat (x,w) { if (y+1 < h and a[ix(y,x)] != a[ix(y+1,x)]) { g[ix(y,x)].insert(ix(y+1,x)); g[ix(y+1,x)].insert(ix(y,x)); } if (x+1 < w and a[ix(y,x)] != a[ix(y,x+1)]) { g[ix(y,x)].insert(ix(y,x+1)); g[ix(y,x+1)].insert(ix(y,x)); } } int q; cin >> q; repeat (query,q) { int y, x, p; cin >> y >> x >> p; -- y; -- x; int i = t.find_root(ix(y,x)); if (a[i] == p) continue; a[i] = p; set<int> s; for (int j : g[i]) { t.union_tree(i,j); for (int k : g[j]) s.insert(k); } for (int j : g[i]) s.erase(j); s.erase(i); int k = t.find_root(i); g[k] = s; for (int j : g[k]) g[j].insert(k); a[k] = p; } repeat (y,h) { repeat (x,w) { if (x) cout << ' '; cout << a[t.find_root(ix(y,x))]; } cout << endl; } return 0; }