結果

問題 No.307 最近色塗る問題多くない?
ユーザー furafura
提出日時 2020-07-28 22:19:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 4,000 ms
コード長 1,561 bytes
コンパイル時間 2,988 ms
コンパイル使用メモリ 213,076 KB
実行使用メモリ 12,904 KB
最終ジャッジ日時 2023-09-11 06:49:24
合計ジャッジ時間 5,606 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 11 ms
4,496 KB
testcase_08 AC 59 ms
11,528 KB
testcase_09 AC 29 ms
7,472 KB
testcase_10 AC 11 ms
4,736 KB
testcase_11 AC 26 ms
6,752 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 8 ms
4,400 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 26 ms
7,008 KB
testcase_18 AC 46 ms
10,444 KB
testcase_19 AC 53 ms
11,568 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 46 ms
12,404 KB
testcase_22 AC 58 ms
12,492 KB
testcase_23 AC 50 ms
12,088 KB
testcase_24 AC 52 ms
12,364 KB
testcase_25 AC 63 ms
11,928 KB
testcase_26 AC 65 ms
12,496 KB
testcase_27 AC 47 ms
12,728 KB
testcase_28 AC 68 ms
12,572 KB
testcase_29 AC 8 ms
4,380 KB
testcase_30 AC 61 ms
12,572 KB
testcase_31 AC 53 ms
12,640 KB
testcase_32 AC 34 ms
12,680 KB
testcase_33 AC 42 ms
12,592 KB
testcase_34 AC 52 ms
12,904 KB
testcase_35 AC 51 ms
12,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class union_find{
	int n;
	vector<int> p;
public:
	union_find(int n):n(n),p(n,-1){}
	int find(int u){ return p[u]<0?u:p[u]=find(p[u]); }
	void unite(int u,int v){
		u=find(u); v=find(v);
		if(u!=v){
			if(p[v]<p[u]) swap(u,v);
			p[u]+=p[v]; p[v]=u; n--;
		}
	}
	bool is_same(int u,int v){ return find(u)==find(v); }
	int size()const{ return n; }
	int size(int u){ return -p[find(u)]; }
};

int main(){
	int h,w; scanf("%d%d",&h,&w);
	vector<int> color(h*w);
	rep(i,h) rep(j,w) scanf("%d",&color[i*w+j]);

	vector<set<int>> G(h*w);
	rep(u,h*w){
		int y=u/w,x=u%w;
		if(y+1<h){
			G[u].emplace((y+1)*w+x);
			G[(y+1)*w+x].emplace(u);
		}
		if(x+1<w){
			G[u].emplace(y*w+(x+1));
			G[y*w+(x+1)].emplace(u);
		}
	}

	union_find U(h*w);
	auto connect=[&](int u,int v){
		u=U.find(u);
		v=U.find(v);
		if(U.is_same(u,v)) return;
		U.unite(u,v);

		if(U.find(u)==v) swap(u,v);
		for(int w:G[v]) if(w!=u) {
			G[w].erase(v);
			G[w].emplace(u);
		}
		G[u].merge(G[v]);
		G[u].erase(u);
		G[u].erase(v);
		G[v].clear();
	};

	rep(u,h*w){
		int y=u/w,x=u%w;
		if(y+1<h && color[u]==color[(y+1)*w+x]) connect(u,(y+1)*w+x);
		if(x+1<w && color[u]==color[y*w+(x+1)]) connect(u,y*w+(x+1));
	}

	int q; scanf("%d",&q);
	rep(_,q){
		int y,x,c; scanf("%d%d%d",&y,&x,&c); y--; x--;

		int u=U.find(y*w+x);
		if(color[u]==c) continue;

		color[u]=c;
		set<int> To=G[u];
		for(int v:To) connect(u,v);
	}

	rep(i,h) rep(j,w) printf("%d%c",color[U.find(i*w+j)],j<w-1?' ':'\n');

	return 0;
}
0