結果

問題 No.307 最近色塗る問題多くない?
ユーザー IL_mstaIL_msta
提出日時 2015-11-28 15:00:55
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,812 bytes
コンパイル時間 1,046 ms
コンパイル使用メモリ 110,704 KB
実行使用メモリ 812,992 KB
最終ジャッジ日時 2023-10-12 05:21:47
合計ジャッジ時間 7,815 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 42 ms
19,080 KB
testcase_28 WA -
testcase_29 RE -
testcase_30 WA -
testcase_31 AC 49 ms
18,816 KB
testcase_32 AC 31 ms
12,580 KB
testcase_33 MLE -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
 
#include <iostream>
#include <iomanip>
#include <sstream>
 
#include <algorithm>
#include <cmath>
 
#include <string>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <valarray>

#include<cassert>//assert();
//#include <random>//xAOJ
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
 
#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
/////////
using namespace::std;
/////////
/////////

class UnionFind{
public:
	int cNum;//要素数
	vector<int> parent;
	vector<int> color;
	vector< set< int > > tonari;
	UnionFind(int n){
		cNum = n;
		parent = vector<int>(n);
		color = vector<int>(n);
		tonari = vector< set<int> >(n);
		for(int i=0;i<n;++i){parent[i] = i;}
	}
	int find(int x){
		if( parent[x] == x ){return x;}
		return parent[x] = find( parent[x] );
	}
	bool same(int x,int y){return find(x) == find(y);}
	void add(int x,int y){//union
		x = find(x);
		y = find(y);
		if( x==y )return;
		parent[x] = y;
		set<int>::iterator it,endit;
		it = tonari[y].begin();
		endit = tonari[y].end();
		for( ;it != endit;++it ){
			tonari[x].insert( *it );
		}
	}
	void tonariAdd(int atti,int kotti){
		tonari[ find(atti) ].insert(kotti);
		tonari[ find(kotti) ].insert(atti);
	}
};

void solve(){
	int H,W;
	cin >> H >> W;
	UnionFind UF(H*W);

	for(int h=0;h<H;++h){
		for(int w=0;w<W;++w){
			cin >> UF.color[h*W + w];
		}
	}
	////////////////
	if( 1<H && UF.color[0] == UF.color[1] ){
		UF.add(0,1);
	}else{
		UF.tonariAdd(0,1);
	}
	if( 1<W && UF.color[0] == UF.color[W] ){
		UF.add(0,W);
	}else{
		UF.tonariAdd(0,W);
	}
	for(int h=1;h<H;++h){
		for(int w=1;w<W;++w){
			if( UF.color[h*W+w] == UF.color[(h-1)*W + w] ){
				UF.add(h*W+w,(h-1)*W+w );
			}else{
				UF.tonariAdd(h*W+w,(h-1)*W+w);
			}
			if( UF.color[h*W+w] == UF.color[h*W+(w-1)] ){
				UF.add( h*W+w,h*W+(w-1) );
			}else{
				UF.tonariAdd(h*W+w,h*W+(w-1) );
			}
		}
	}
	////////////////
	int Q;
	cin >> Q;
	int R,C,X;
	set<int >::iterator it,endit;
	int ter;
	set<int> tempset;
	while(Q--){
		cin >> R >> C >> X;
		--R;--C;
		ter = UF.find(R*W+C);
		if( UF.color[ ter ] == X ) continue;
		UF.color[ter] = X;
		tempset = UF.tonari[ ter ];
		it = tempset.begin();
		endit = tempset.end();
		for(;it != endit;++it){
			UF.add( ter, *it);
		}
	}
	for(int h=0;h<H;++h){
		for(int w = 0;w<W;++w){
			if( w != 0 ){
				cout << ' ';
			}
			if( UF.color[ UF.find(h*W+w) ] ){
				cout << 1;
			}else{
				cout << 0;
			}
		}
		cout << endl;
	}
}
int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(16);//
	
	//cpp
	solve();
	return 0;
}
0