結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
IL_msta
|
| 提出日時 | 2018-02-16 22:42:51 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,462 bytes |
| コンパイル時間 | 1,060 ms |
| コンパイル使用メモリ | 108,452 KB |
| 実行使用メモリ | 197,120 KB |
| 最終ジャッジ日時 | 2024-06-23 03:50:48 |
| 合計ジャッジ時間 | 11,694 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 12 WA * 24 |
ソースコード
#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[x] = y;
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 << ' ';
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()
IL_msta