結果

問題 No.307 最近色塗る問題多くない?
ユーザー IL_mstaIL_msta
提出日時 2018-02-16 23:15:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 303 ms / 4,000 ms
コード長 3,771 bytes
コンパイル時間 1,095 ms
コンパイル使用メモリ 115,816 KB
実行使用メモリ 14,976 KB
最終ジャッジ日時 2024-05-01 17:06:00
合計ジャッジ時間 3,320 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 15 ms
6,940 KB
testcase_08 AC 74 ms
13,312 KB
testcase_09 AC 26 ms
8,704 KB
testcase_10 AC 9 ms
5,376 KB
testcase_11 AC 20 ms
7,680 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 12 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 18 ms
7,808 KB
testcase_18 AC 31 ms
10,880 KB
testcase_19 AC 35 ms
12,544 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 33 ms
12,800 KB
testcase_22 AC 42 ms
13,184 KB
testcase_23 AC 37 ms
12,288 KB
testcase_24 AC 37 ms
12,416 KB
testcase_25 AC 49 ms
12,288 KB
testcase_26 AC 47 ms
12,544 KB
testcase_27 AC 37 ms
12,800 KB
testcase_28 AC 303 ms
14,080 KB
testcase_29 AC 8 ms
5,376 KB
testcase_30 AC 49 ms
12,800 KB
testcase_31 AC 44 ms
12,928 KB
testcase_32 AC 30 ms
12,928 KB
testcase_33 AC 40 ms
14,848 KB
testcase_34 AC 49 ms
14,848 KB
testcase_35 AC 51 ms
14,976 KB
権限があれば一括ダウンロードができます

ソースコード

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;

class unionfind_set{

	int cNum;//要素数
	vector< set<int> > other;
	vector<int> parent;
	vector<int> color;
public:
	unionfind_set(int n){
		cNum = n;
		parent = vector<int>(n);
		other = vector< set<int> >(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].insert(rr*W+cc);
				}
			}
		}
	}
	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;
		if( other[x].size() < other[y].size() ){
			swap(x,y);
		}

		parent[y] = x;
		set<int>::iterator itr,end;
		itr = other[y].begin();
		end = other[y].end();
		for(;itr!=end;++itr){
			other[x].insert( find(*itr) );
		}

		other[x].erase(x);
		other[x].erase(y);
	}
	void grow(int x,int color){
		x = find( x );
		set<int> temp = other[x];
		set_color(x,color);
		set<int>::iterator itr,end;
		itr = temp.begin();
		end = temp.end();
		for(;itr!=end;++itr){
			add(*itr,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