結果

問題 No.307 最近色塗る問題多くない?
ユーザー Kmcode1Kmcode1
提出日時 2015-11-27 23:21:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,630 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 111,216 KB
実行使用メモリ 16,324 KB
最終ジャッジ日時 2023-10-12 00:46:48
合計ジャッジ時間 3,385 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,876 KB
testcase_01 AC 4 ms
5,984 KB
testcase_02 AC 4 ms
6,120 KB
testcase_03 AC 3 ms
5,884 KB
testcase_04 AC 5 ms
6,064 KB
testcase_05 AC 4 ms
6,016 KB
testcase_06 AC 4 ms
5,916 KB
testcase_07 AC 8 ms
6,520 KB
testcase_08 AC 29 ms
8,568 KB
testcase_09 AC 15 ms
7,384 KB
testcase_10 AC 8 ms
6,512 KB
testcase_11 AC 14 ms
7,308 KB
testcase_12 AC 4 ms
6,020 KB
testcase_13 AC 15 ms
6,068 KB
testcase_14 AC 5 ms
6,204 KB
testcase_15 AC 7 ms
6,280 KB
testcase_16 AC 6 ms
6,280 KB
testcase_17 AC 16 ms
8,116 KB
testcase_18 AC 32 ms
10,880 KB
testcase_19 WA -
testcase_20 AC 5 ms
6,340 KB
testcase_21 WA -
testcase_22 AC 54 ms
15,052 KB
testcase_23 AC 51 ms
15,684 KB
testcase_24 WA -
testcase_25 AC 62 ms
15,548 KB
testcase_26 AC 64 ms
15,948 KB
testcase_27 AC 48 ms
16,112 KB
testcase_28 AC 66 ms
16,324 KB
testcase_29 AC 11 ms
5,876 KB
testcase_30 AC 64 ms
16,280 KB
testcase_31 WA -
testcase_32 AC 43 ms
15,880 KB
testcase_33 AC 16 ms
7,212 KB
testcase_34 AC 26 ms
7,100 KB
testcase_35 AC 26 ms
7,184 KB
権限があれば一括ダウンロードができます

ソースコード

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];
bool 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]);
		//swap(cl[a], cl[b]);
		//swap(recently[a], recently[b]);
	}
	for (auto ite = mp[b].begin();ite != mp[b].end();ite++) {
		mp[a].insert(root(*ite));
	}
	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;
void change(int b) {
	cl[b] ^= true;
	recently[b] = ope;
	cp.clear();
	vector<pair<int, int> > V;
	for (auto ite = mp[b].begin();ite != mp[b].end();ite++) {
		if (cl[root((*ite))] == cl[b]) {
			V.push_back(make_pair((*ite), b));
		}
		else {
			cp.insert((root(*ite)));
		}
	}
	swap(mp[b], cp);
	for (int i = 0;i < V.size();i++) {
		merge(V[i].first, V[i].second);
	}
}
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]);
			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;
		}
		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