結果

問題 No.307 最近色塗る問題多くない?
ユーザー pekempeypekempey
提出日時 2015-11-27 23:33:07
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,277 bytes
コンパイル時間 1,399 ms
コンパイル使用メモリ 159,004 KB
実行使用メモリ 16,740 KB
最終ジャッジ日時 2023-10-12 00:56:15
合計ジャッジ時間 9,835 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,704 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,356 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 56 ms
4,352 KB
testcase_08 AC 1,340 ms
6,808 KB
testcase_09 AC 143 ms
5,040 KB
testcase_10 AC 6 ms
4,348 KB
testcase_11 AC 14 ms
4,724 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 15 ms
4,348 KB
testcase_14 AC 3 ms
4,352 KB
testcase_15 AC 4 ms
4,352 KB
testcase_16 AC 4 ms
4,352 KB
testcase_17 AC 19 ms
5,480 KB
testcase_18 AC 55 ms
8,328 KB
testcase_19 AC 56 ms
9,512 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 56 ms
12,756 KB
testcase_22 AC 57 ms
12,152 KB
testcase_23 AC 242 ms
12,236 KB
testcase_24 AC 119 ms
12,868 KB
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define chmin(a, b) ((b) < a && (a = (b), true))
#define chmax(a, b) (a < (b) && (a = (b), true))
using namespace std;
typedef long long ll;

int aa[101010];

struct UF {
	vector<int> parent, color;
	vector<set<int>> g;
	UF(int n) : parent(n), color(n), g(n) {
		rep (i, n) parent[i] = i;
	}
	int root(int x) {
		if (x == parent[x]) return x;
		else return parent[x] = root(parent[x]);
	}
	void merge(int x, int y) {
		x = root(x); y = root(y);
		if (x == y) return;
		if (g[x].size() < g[y].size()) swap(x, y);
		parent[y] = x;
		auto it = g[y].begin();
		while (it != g[y].end()) {
			if (root(*it) == x) {
				g[y].erase(it++);
			} else ++it;
		}
		g[x].insert(g[y].begin(), g[y].end());
		g[y].clear();
	}
	bool same(int x, int y) {
		return root(x) == root(y);
	}
	void add(int u, int v) {
		u = root(u); v = root(v);
		g[u].insert(v);
		g[v].insert(u);
	}
	void update(int x, int c) {
		x = root(x);
		if (color[x] == c) return;
		color[x] = c;
		int size = 0;
		for (int y : g[x]) aa[size++] = y;

		rep (i, size) if (getcolor(root(aa[i])) == c && x != root(aa[i])) {
			merge(x, aa[i]);
		}
	}
	int getcolor(int x) {
		x = root(x);
		return color[x];
	}
};

int g[200][200];
int A[200][200];

int main() {
	int h, w;
	cin >> h >> w;
	UF uf(h * w);
	rep (i, h) rep (j, w) cin >> A[i][j];
	rep (i, h) rep (j, w) uf.color[i * w + j] = A[i][j];
	rep (i, h) rep (j, w) {
		int dy[] = {1, 0};
		int dx[] = {0, 1};
		rep (k, 2) {
			int ny = i + dy[k];
			int nx = j + dx[k];
			if (ny < h && nx < w) {
				if (A[ny][nx] == A[i][j]) {
					uf.merge(ny * w + nx, i * w + j);
				} else {
					uf.add(ny * w + nx, i * w + j);
				}
			}
		}
	}

	int q;
	cin >> q;
	rep (i, q) {
		int r, c, x;
		scanf("%d %d %d", &r, &c, &x);
		r--; c--;
		uf.update(r * w + c, x);
	}

	rep (i, h) {
		rep (j, w) {
			if (j) cout << " ";
			cout << uf.getcolor(i * w + j);
		}
		cout << endl;
	}
	return 0;
}
0