結果

問題 No.307 最近色塗る問題多くない?
ユーザー furafura
提出日時 2020-07-28 21:39:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,461 bytes
コンパイル時間 2,769 ms
コンパイル使用メモリ 211,572 KB
実行使用メモリ 12,804 KB
最終ジャッジ日時 2023-09-11 06:45:23
合計ジャッジ時間 5,152 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 9 ms
4,716 KB
testcase_11 AC 23 ms
6,852 KB
testcase_12 WA -
testcase_13 AC 10 ms
4,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 4 ms
4,380 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 44 ms
12,760 KB
testcase_28 WA -
testcase_29 AC 8 ms
4,380 KB
testcase_30 WA -
testcase_31 AC 49 ms
12,628 KB
testcase_32 AC 34 ms
12,636 KB
testcase_33 WA -
testcase_34 AC 70 ms
12,632 KB
testcase_35 AC 88 ms
12,772 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);
		U.unite(u,v);

		if(U.find(u)==v) swap(u,v);
		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