結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-03-01 22:18:04 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,300 bytes |
| コンパイル時間 | 1,055 ms |
| コンパイル使用メモリ | 78,640 KB |
| 実行使用メモリ | 19,612 KB |
| 最終ジャッジ日時 | 2024-09-24 13:14:03 |
| 合計ジャッジ時間 | 8,778 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 TLE * 1 -- * 7 |
ソースコード
#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;
for (int j : g[i]) t.union_tree(i,j);
int l = t.find_root(i);
set<int> s;
for (int j : g[i]) {
for (int k : g[j]) if (k != i) {
g[k].erase(i);
g[k].erase(j);
g[k].insert(l);
s.insert(k);
}
}
g[l] = s;
a[l] = p;
}
repeat (y,h) {
repeat (x,w) {
if (x) cout << ' ';
cout << a[root(y,x)];
}
cout << endl;
}
return 0;
}