結果

問題 No.307 最近色塗る問題多くない?
ユーザー 👑 kmjpkmjp
提出日時 2015-11-27 23:04:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,037 ms / 4,000 ms
コード長 2,281 bytes
コンパイル時間 1,712 ms
コンパイル使用メモリ 163,120 KB
実行使用メモリ 169,468 KB
最終ジャッジ日時 2023-08-14 03:44:16
合計ジャッジ時間 5,209 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
8,104 KB
testcase_01 AC 7 ms
7,856 KB
testcase_02 AC 7 ms
7,864 KB
testcase_03 AC 7 ms
8,116 KB
testcase_04 AC 8 ms
8,188 KB
testcase_05 AC 8 ms
8,112 KB
testcase_06 AC 8 ms
8,188 KB
testcase_07 AC 11 ms
8,288 KB
testcase_08 AC 29 ms
9,688 KB
testcase_09 AC 17 ms
9,020 KB
testcase_10 AC 13 ms
8,564 KB
testcase_11 AC 31 ms
10,928 KB
testcase_12 AC 7 ms
8,112 KB
testcase_13 AC 17 ms
8,176 KB
testcase_14 AC 8 ms
8,104 KB
testcase_15 AC 9 ms
7,860 KB
testcase_16 AC 9 ms
8,048 KB
testcase_17 AC 15 ms
9,204 KB
testcase_18 AC 21 ms
9,280 KB
testcase_19 AC 24 ms
9,668 KB
testcase_20 AC 8 ms
8,108 KB
testcase_21 AC 22 ms
9,488 KB
testcase_22 AC 25 ms
9,424 KB
testcase_23 AC 22 ms
9,348 KB
testcase_24 AC 22 ms
9,352 KB
testcase_25 AC 33 ms
9,496 KB
testcase_26 AC 32 ms
9,664 KB
testcase_27 AC 88 ms
24,936 KB
testcase_28 AC 1,037 ms
169,468 KB
testcase_29 AC 15 ms
7,864 KB
testcase_30 AC 32 ms
9,740 KB
testcase_31 AC 121 ms
32,092 KB
testcase_32 AC 29 ms
9,544 KB
testcase_33 AC 25 ms
10,344 KB
testcase_34 AC 35 ms
10,340 KB
testcase_35 AC 35 ms
10,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

template<int um> class UF {
	public:
	vector<int> par,rank;
	UF() {rank=vector<int>(um,0); for(int i=0;i<um;i++) par.push_back(i);}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		if(rank[x]>rank[y]) return par[x]=y;
		rank[x]+=rank[x]==rank[y]; return par[y]=x;
	}
};

UF<500000> uf;

int H,W,Q;
int A[201*201];
vector<int> S[201*201];
int id(int y,int x){ return y*W+x;}



void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>H>>W;
	FOR(y,H) FOR(x,W) {
		cin>>A[id(y,x)];
	}
	FOR(y,H) FOR(x,W) {
		if(y) {
			if(A[id(y,x)]==A[id(y-1,x)]) uf(id(y,x),id(y-1,x));
			else S[id(y,x)].push_back(id(y-1,x));
		}
		if(y<H-1) {
			if(A[id(y,x)]==A[id(y+1,x)]) uf(id(y,x),id(y+1,x));
			else S[id(y,x)].push_back(id(y+1,x));
		}
		if(x) {
			if(A[id(y,x)]==A[id(y,x-1)]) uf(id(y,x),id(y,x-1));
			else S[id(y,x)].push_back(id(y,x-1));
		}
		if(x<W-1) {
			if(A[id(y,x)]==A[id(y,x+1)]) uf(id(y,x),id(y,x+1));
			else S[id(y,x)].push_back(id(y,x+1));
		}
	}
	
	FOR(x,H*W) if(uf[x]!=x) FORR(r,S[x]) S[uf[x]].push_back(r);
	FOR(x,H*W) if(uf[x]==x) {
		sort(ALL(S[x]));
		S[x].erase(unique(ALL(S[x])),S[x].end());
	}
	cin>>Q;
	while(Q--) {
		cin>>y>>x>>k;
		x=uf[id(y-1,x-1)];
		if(A[x]==k) continue;
		if(S[x].size()) {
			vector<int> NEW;
			FORR(r,S[x]) if(uf[x]!=uf[r]) {
				FORR(r2,S[uf[r]]) if(uf[r2]!=uf[x]) NEW.push_back(r2);
				uf(x,r);
			}
			sort(ALL(NEW));
			NEW.erase(unique(ALL(NEW)),NEW.end());
			S[uf[x]]=NEW;
		}
		A[uf[x]]=k;
	}
	FOR(y,H) {
		FOR(x,W) _P("%d%c",A[uf[id(y,x)]],(x==W-1)?'\n':' ');
	}
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0