結果
問題 |
No.307 最近色塗る問題多くない?
|
ユーザー |
|
提出日時 | 2016-03-01 21:50:15 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,249 bytes |
コンパイル時間 | 922 ms |
コンパイル使用メモリ | 78,996 KB |
実行使用メモリ | 13,348 KB |
最終ジャッジ日時 | 2024-09-24 13:12:21 |
合計ジャッジ時間 | 7,068 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 8 TLE * 1 -- * 27 |
ソースコード
#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)); } auto root = [&](int y, int x) { return t.find_root(ix(y,x)); }; 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[root(y,x)].insert(root(y+1,x)); g[root(y+1,x)].insert(root(y,x)); } if (x+1 < w and a[ix(y,x)] != a[ix(y,x+1)]) { g[root(y,x)].insert(root(y,x+1)); g[root(y,x+1)].insert(root(y,x)); } } int q; cin >> q; repeat (query,q) { int y, x, p; cin >> y >> x >> p; -- y; -- x; int i = root(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[root(y,x)]; } cout << endl; } return 0; }