結果

問題 No.307 最近色塗る問題多くない?
ユーザー IL_mstaIL_msta
提出日時 2018-02-16 22:50:51
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,624 bytes
コンパイル時間 1,077 ms
コンパイル使用メモリ 106,972 KB
実行使用メモリ 193,956 KB
最終ジャッジ日時 2023-09-05 08:07:30
合計ジャッジ時間 9,868 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 6 ms
9,552 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 77 ms
30,520 KB
testcase_08 AC 1,390 ms
175,052 KB
testcase_09 AC 170 ms
95,016 KB
testcase_10 AC 22 ms
33,632 KB
testcase_11 AC 63 ms
80,476 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 18 ms
4,376 KB
testcase_14 AC 10 ms
16,888 KB
testcase_15 AC 15 ms
25,052 KB
testcase_16 AC 10 ms
17,356 KB
testcase_17 AC 52 ms
84,556 KB
testcase_18 AC 92 ms
144,456 KB
testcase_19 AC 106 ms
174,984 KB
testcase_20 AC 8 ms
15,312 KB
testcase_21 AC 106 ms
192,156 KB
testcase_22 AC 117 ms
193,956 KB
testcase_23 AC 142 ms
185,476 KB
testcase_24 AC 134 ms
189,264 KB
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

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;

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