結果

問題 No.307 最近色塗る問題多くない?
ユーザー FF256grhyFF256grhy
提出日時 2016-05-08 18:03:21
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,724 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 26,880 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 13:52:36
合計ジャッジ時間 2,830 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 9 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 12 ms
5,376 KB
testcase_18 AC 29 ms
5,376 KB
testcase_19 AC 27 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 22 ms
5,376 KB
testcase_22 AC 22 ms
6,144 KB
testcase_23 AC 140 ms
6,216 KB
testcase_24 AC 55 ms
5,376 KB
testcase_25 AC 84 ms
6,656 KB
testcase_26 AC 157 ms
6,912 KB
testcase_27 AC 15 ms
5,376 KB
testcase_28 WA -
testcase_29 AC 8 ms
5,376 KB
testcase_30 AC 160 ms
6,912 KB
testcase_31 AC 21 ms
5,376 KB
testcase_32 AC 20 ms
5,376 KB
testcase_33 WA -
testcase_34 AC 20 ms
5,376 KB
testcase_35 AC 21 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:96:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   96 |         scanf("%d%d", &h, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~
main.cpp:99:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   99 |                 scanf("%d", &a[i][j]);
      |                 ~~~~~^~~~~~~~~~~~~~~~
main.cpp:102:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  102 |         scanf("%d", &q);
      |         ~~~~~^~~~~~~~~~
main.cpp:104:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  104 |                 scanf("%d%d%d", &r[i], &c[i], &x[i]);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

// UnionFind のスニペットを作ったので試しに使ってみる

#include <stdio.h>

class UnionFind {
public:
	UnionFind(int size);
	int unite(int v, int w);
	int find(int v);
	int get_size_eq(int v);
	int get_size_part();
	virtual ~UnionFind();

private:
	int *parent;
	int *size_eq;
	int size_part;
};

UnionFind::UnionFind(int size) {
	parent = new int[size];
	size_eq = new int[size];
	for(int i = 0; i < size; i++) { parent[i] = -1; size_eq[i] = 1; }
	size_part = size;
}

int UnionFind::unite(int v, int w) {
	int fv = find(v);
	int fw = find(w);
	if(fv == fw) { return fv; }
	
	size_part--;
	int &sv = size_eq[fv];
	int &sw = size_eq[fw];
	
	if(sv < sw) {
		parent[fv] = fw;
		sw += sv;
		return fw;
	} else {
		parent[fw] = fv;
		sv += sw;
		return fv;
	}
}

int UnionFind::find(int v) {
	if(parent[v] == -1) { return v; }
	return parent[v] = find( parent[v] );
}

int UnionFind::get_size_eq(int v) {
	return size_eq[ find(v) ];
}

int UnionFind::get_size_part() {
	return size_part;
}

UnionFind::~UnionFind() {
	delete[] parent;
	delete[] size_eq;
}




int h, w, a[200][200], q, r[50000], c[50000], x[50000];
UnionFind *uf;
int neighbor[200 * 200], ptr;

int at(int i, int j) {
	return i * w + j;
}

void dfs(int i, int j, int f, int v) {
	if( ! ( 0 <= i && i < h && 0 <= j && j < w) ) { return; }
	if(a[i][j] == v) { return; }
	a[i][j] = v;
	
	int fij = uf -> find( at(i, j) );
	if(fij == f) {
		dfs(i - 1, j    , f, v);
		dfs(i + 1, j    , f, v);
		dfs(i    , j - 1, f, v);
		dfs(i    , j + 1, f, v);
	} else {
		neighbor[ptr] = fij;
		ptr++;
	}
	
	return;
}

int main() {
	scanf("%d%d", &h, &w);
	for(int i = 0; i < h; i++) {
	for(int j = 0; j < w; j++) {
		scanf("%d", &a[i][j]);
	}
	}
	scanf("%d", &q);
	for(int i = 0; i < q; i++) {
		scanf("%d%d%d", &r[i], &c[i], &x[i]);
		r[i]--;
		c[i]--;
	}
	
	
	uf = new UnionFind(h * w);
	
	for(int i = 0; i < h; i++) {
	for(int j = 0; j < w - 1; j++) {
		if(a[i][j] == a[i][j + 1]) { uf -> unite( at(i, j), at(i, j + 1) ); }
	}
	}
	for(int j = 0; j < w; j++) {
	for(int i = 0; i < h - 1; i++) {
		if(a[i][j] == a[i + 1][j]) { uf -> unite( at(i, j), at(i + 1, j) ); }
	}
	}
	
	
	for(int k = 0; k < q; k++) {
		if(uf -> get_size_part() == 1) {
			a[0][0] = x[k];
		} else {
			int f = uf -> find( at(r[k], c[k]) );
			int i = f / w;
			int j = f % w;
			if(a[i][j] % 2 != x[k] % 2) {
				ptr = 0;
				dfs(i, j, f, 2 * (k + 1) + x[k]);
				for(int l = 0; l < ptr; l++) {
					f = uf -> unite(f, neighbor[l]);
				}
				i = f / w;
				j = f % w;
				a[i][j] = x[k];
			}
		}
	}
	
	
	for(int i = 0; i < h; i++) {
	for(int j = 0; j < w; j++) {
		int f = uf -> find( at(i, j) );
		printf("%d ", a[f / w][f % w] );
	} printf("\n");
	}
	
	delete uf;
	
	return 0;
}
0