結果

問題 No.307 最近色塗る問題多くない?
ユーザー IL_mstaIL_msta
提出日時 2018-02-16 23:15:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 310 ms / 4,000 ms
コード長 3,771 bytes
コンパイル時間 1,789 ms
コンパイル使用メモリ 117,180 KB
実行使用メモリ 14,864 KB
最終ジャッジ日時 2023-08-14 04:17:55
合計ジャッジ時間 4,006 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 17 ms
4,716 KB
testcase_08 AC 79 ms
13,396 KB
testcase_09 AC 30 ms
8,604 KB
testcase_10 AC 10 ms
4,912 KB
testcase_11 AC 23 ms
7,520 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 13 ms
4,380 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 7 ms
4,544 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 21 ms
7,944 KB
testcase_18 AC 38 ms
10,920 KB
testcase_19 AC 43 ms
12,480 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 40 ms
12,656 KB
testcase_22 AC 49 ms
13,164 KB
testcase_23 AC 42 ms
12,220 KB
testcase_24 AC 45 ms
12,396 KB
testcase_25 AC 59 ms
12,080 KB
testcase_26 AC 58 ms
12,488 KB
testcase_27 AC 40 ms
12,736 KB
testcase_28 AC 310 ms
14,124 KB
testcase_29 AC 8 ms
4,380 KB
testcase_30 AC 56 ms
12,776 KB
testcase_31 AC 46 ms
12,904 KB
testcase_32 AC 32 ms
12,744 KB
testcase_33 AC 43 ms
14,864 KB
testcase_34 AC 52 ms
14,592 KB
testcase_35 AC 52 ms
14,844 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