結果

問題 No.307 最近色塗る問題多くない?
ユーザー Kmcode1Kmcode1
提出日時 2015-11-27 23:52:20
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 3,031 bytes
コンパイル時間 964 ms
コンパイル使用メモリ 112,632 KB
実行使用メモリ 814,964 KB
最終ジャッジ日時 2023-10-12 01:40:54
合計ジャッジ時間 4,242 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
#include<functional>
#include<climits>
#include<cassert>
#include<iterator>
#include<unordered_map>
#include<unordered_set>

using namespace std;

#define MAX 202

pair<int, int> co[MAX*MAX];
int cnt[MAX][MAX];
int recently[MAX*MAX];
int belong[MAX*MAX];
unordered_set<int> mp[MAX*MAX];
int cl[MAX*MAX];
inline int root(int b) {
	if (belong[b] == -1) {
		return b;
	}
	belong[b] = root(belong[b]);
	return belong[b];
}
void merge(int a, int b) {
	a = root(a);
	b = root(b);
	if (a == b) {
		return;
	}
	if (recently[a] > recently[b]) {

	}
	else {
		swap(a, b);
	}
	if (mp[a].size() < mp[b].size()) {
		swap(mp[a], mp[b]);
	}
	vector<pair<int, int> > ich;
	for (auto ite = mp[b].begin();ite != mp[b].end();ite++) {
		if (cl[root(*ite)] != cl[b]) {
			mp[a].insert(root(*ite));
		}
		else {
			if (root(*ite) != b) {
				ich.push_back(make_pair((*ite), b));
			}
		}
	}
	for (int i = 0;i < ich.size();i++) {
		merge(ich[i].first, ich[i].second);
	}
	mp[b].clear();
	belong[b] = a;
}

int k[MAX][MAX];
int h;
int w;

int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };

unordered_set<int> cp;
int ope = 0;
int want;
void change(int b) {
	cl[b] = want;
	recently[b] = ope;
	bool update = true;
	while (update) {
		update = false;
		cp.clear();
		vector<pair<int, int> > V;
		for (auto ite = mp[b].begin();ite != mp[b].end();ite++) {
			if (root(*ite)!=b&&cl[root((*ite))] == cl[b]) {
				V.push_back(make_pair(root((*ite)), b));
			}
			else {
				cp.insert((root(*ite)));
			}
		}
		swap(mp[b], cp);
		for (int i = 0;i < V.size();i++) {
			update = true;
			merge(V[i].first, V[i].second);
		}
		update = false;
	}
}
int main() {
	cin >> h >> w;
	int C = 0;
	for (int i = 0;i < h;i++) {
		for (int j = 0;j < w;j++) {
			scanf("%d", &k[i][j]);
			if (k[i][j]>1) {
				exit(1);
			}
			if (k[i][j] < 0) {
				exit(1);
			}
			cnt[i][j] = C;
			C++;
			co[C - 1] = make_pair(i, j);
			cl[C - 1] = k[i][j];
		}
	}
	memset(belong, -1, sizeof(belong));
	for (int i = 0;i < h;i++) {
		for (int j = 0;j < w;j++) {
			for (int k1 = 0;k1 < 4;k1++) {
				int ii = i + dx[k1];
				int jj = j + dy[k1];
				if (ii >= 0 && jj >= 0 && ii < h&&jj < w&&k[i][j] == k[ii][jj]) {
					merge(cnt[ii][jj], cnt[i][j]);
				}
				else {
					mp[root(cnt[i][j])].insert(root(cnt[ii][jj]));
				}
			}
		}
	}
	int q;
	scanf("%d", &q);
	while (q--) {
		ope++;
		int a, b;
		scanf("%d%d", &a, &b);
		a--;
		b--;
		int x;
		scanf("%d", &x);
		int f = root(cnt[a][b]);
		if (cl[f] == x) {
			continue;
		}
		want = x;
		change(f);
	}
	for (int i = 0;i < h;i++) {
		for (int j = 0;j < w;j++) {
			int f = cl[root(cnt[i][j])];
			if (j) {
				printf(" ");
			}
			printf("%d", f);
		}
		puts("");
	}
	return 0;
}
0