結果

問題 No.307 最近色塗る問題多くない?
ユーザー IL_mstaIL_msta
提出日時 2018-02-16 22:50:05
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 3,652 bytes
コンパイル時間 1,002 ms
コンパイル使用メモリ 108,548 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-05 08:06:17
合計ジャッジ時間 6,832 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma region include
#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <complex>

#include <string>
#include <cstring>
#include <vector>
#include <tuple>
#include <bitset>

#include <queue>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <fstream>
#include <random>
//#include <time.h>
//#include <intrin.h>
#include <ctime>
#pragma endregion //#include
/////////

#pragma region typedef
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef std::pair<LL,LL> PLL;//
typedef std::pair<int,int> PII;//
#pragma endregion //typedef
////定数
const int INF = (int)1e9;
const LL MOD = (LL)1e9+7;
const LL LINF = (LL)4e18+20;
const LD PI = acos(-1.0);
const double EPS = 1e-9;
/////////
using namespace::std;

int H,W;
vector< vector<int> > field;

//const int UF_SET_MAX = 40000;
const int UF_SET_MAX = 16;
class unionfind_set{

	int cNum;//要素数
	vector< bitset<UF_SET_MAX> > other;
	vector<int> parent;
	vector<int> color;
public:
	unionfind_set(int n){
		cNum = n;
		parent = vector<int>(n);
		other = vector< bitset<UF_SET_MAX> >(n);
		color = vector<int>(n);
		for(int i=0;i<n;++i){
			parent[i] = i;
		}
		int dr[] = {0,1,0,-1};
		int dc[] = {1,0,-1,0};
		for(int r=0;r<H;++r){
			for(int c=0;c<W;++c){
				for(int k=0;k<4;++k){
					int rr = r+dr[k];
					int cc = c+dc[k];
					if(rr<0 || H <= rr )continue;
					if(cc<0 || W <= cc ) continue;
					other[r*W+c].set(rr*W+cc,true);
				}
			}
		}
	}
	void set_color(int x,int col){
		x = find(x);
		color[x] = col;
	}
	int get_color(int x){
		x = find(x);
		return color[ x ];
	}
	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){
		x = find(x);
		y = find(y);
		if( x==y ) return;
		parent[y] = x;

		other[x] = other[x] | other[y];
		other[x].set(x,false);
		other[x].set(y,false);
	}
	void grow(int x,int color){
		x = find( x );
		bitset<UF_SET_MAX> temp = other[x];
		set_color(x,color);
		for(int i=0;i<cNum;++i){
			if( temp[i] ){
				add(i,x);
			}
		}
	}
};
void solve(){
	cin >> H >> W;
	field = vector< vector<int> >(H,vector<int>(W));
	unionfind_set ufs(H*W);
	for(int h=0;h<H;++h){
		for(int w=0;w<W;++w){
			cin >> field[h][w];
		}
	}
	for(int r=0;r<H;++r){
		for(int c=0;c<W;++c){
			ufs.set_color(r*W+c,field[r][c]);
			if( r+1<H ){
				if( field[r][c]==field[r+1][c] ){
					ufs.add(r*W+c,(r+1)*W+c);
				}
			}
			if( c+1<W ){
				if( field[r][c] == field[r][c+1] ){
					ufs.add(r*W+c,r*W+(c+1));
				}
			}
		}
	}
	int Q;
	cin >> Q;
	int R,C,X;
	while(Q--){
		cin >> R >> C >> X;
		--R;--C;
		if( ufs.get_color( R*W+C ) != X ){
			ufs.grow(R*W+C, X);
		}
		/*{
			for(int r=0;r<H;++r){
				for(int c=0;c<W;++c){
					if( c ) cout << ' ';
					if( ufs.get_color( r*W+c ) == 0 ){
						cout << 0;
					}else{
						cout << 1;
					}
				}
				cout << '\n';
			}
			cout << flush;
			for(int r=0;r<H;++r){
				for(int c=0;c<W;++c){
					if( c ) cout << ' ';
					cout << ufs.find(r*W+c);
				}
				cout << '\n';
			}
			cout << "--" << endl;
		}*/
	}
	for(int r=0;r<H;++r){
		for(int c=0;c<W;++c){
			if( c ) cout << ' ';
			if( ufs.get_color( r*W+c ) == 0 ){
				cout << 0;
			}else{
				cout << 1;
			}
		}
		cout << '\n';
	}
	cout << flush;
}
#pragma region main
signed main(void){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	std::cout << std::fixed;//小数を10進数表示
	cout << setprecision(16);//小数点以下の桁数を指定//coutとcerrで別	

	solve();
}
#pragma endregion //main()
0