結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-22 18:12:30 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,591 bytes |
| コンパイル時間 | 1,975 ms |
| コンパイル使用メモリ | 184,148 KB |
| 実行使用メモリ | 19,744 KB |
| 最終ジャッジ日時 | 2024-12-14 14:33:20 |
| 合計ジャッジ時間 | 31,843 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 31 TLE * 5 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;
template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
if( x > v ){
x = v;
return 1;
}
return 0;
}
template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
if( x < v ){
x = v;
return 1;
}
return 0;
}
const int INF = 0x3f3f3f3f;
const int MAXH = 200;
const int MAXW = 200;
int H, W;
int A[ MAXH ][ MAXW ];
void init(){
cin >> H >> W;
for( int i = 0; i < H; ++i )
for( int j = 0; j < W; ++j )
cin >> A[ i ][ j ];
}
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };
int cc_col[ MAXH * MAXW ];
struct dsu{
vi fa;
dsu( int sz ){
fa = vi( sz );
for( int i = 0; i < sz; ++i ){
fa[ i ] = i;
}
}
int find( int x ){
return fa[ x ] == x ? x : find( fa[ x ] );
}
int unite( int x, int y ){
int a = find( x );
int b = find( y );
if( a == b )
return 0;
fa[ b ] = a;
return 1;
}
} *ccid_uf;
set< int > cc_graph[ MAXH * MAXW ];
void preprocess(){
ccid_uf = new dsu( H * W );
for( int i = 0; i < H; ++i ){
for( int j = 0; j < W; ++j ){
if( ccid_uf->find( i * W + j ) != i * W + j ) continue;
queue< tuple< int, int > > que;
que.emplace( i, j );
cc_col[ i * W + j ] = A[ i ][ j ];
while( not que.empty() ){
int x, y; tie( x, y ) = que.front(); que.pop();
for( int di = 0; di < 4; ++di ){
int nx = x + dx[ di ];
int ny = y + dy[ di ];
if( not ( 0 <= nx and nx < H and 0 <= ny and ny < W ) ) continue;
if( A[ x ][ y ] != A[ nx ][ ny ] ) continue;
if( ccid_uf->find( nx * W + ny ) == i * W + j ) continue;
ccid_uf->unite( i * W + j, nx * W + ny );
que.emplace( nx, ny );
}
}
}
}
for( int i = 0; i < H; ++i ){
for( int j = 0; j < W; ++j ){
for( int di = 0; di < 4; ++di ){
int nx = i + dx[ di ];
int ny = j + dy[ di ];
if( not ( 0 <= nx and nx < H and 0 <= ny and ny < W ) ) continue;
if( A[ i ][ j ] == A[ nx ][ ny ] ) continue;
cc_graph[ ccid_uf->find( i * W + j ) ].emplace( ccid_uf->find( nx * W + ny ) );
}
}
}
}
void solve(){
int Q; cin >> Q;
for( int i = 0; i < Q; ++i ){
int R, C, X; cin >> R >> C >> X; --R, --C;
if( cc_col[ ccid_uf->find( R * W + C ) ] == X ) continue;
cc_col[ ccid_uf->find( R * W + C ) ] = X;
set< int > vis;
for( int adj_cc : cc_graph[ ccid_uf->find( R * W + C ) ] ){
for( int adj_adj_cc : cc_graph[ adj_cc ] ){
if( adj_adj_cc == ccid_uf->find( R * W + C ) ) continue;
if( vis.count( adj_adj_cc ) ) continue;
cc_graph[ adj_adj_cc ].erase( adj_cc );
cc_graph[ adj_adj_cc ].emplace( ccid_uf->find( R * W + C ) );
vis.emplace( adj_adj_cc );
}
ccid_uf->unite( R * W + C, adj_cc );
cc_graph[ adj_cc ].clear();
}
for( int adj_adj_cc : vis ){
cc_graph[ ccid_uf->find( R * W + C ) ].emplace( adj_adj_cc );
}
}
for( int i = 0; i < H; ++i ){
for( int j = 0; j < W; ++j ){
// cout << ccid_uf->find( i * W + j ) << " \n"[ j + 1 == W ];
cout << cc_col[ ccid_uf->find( i * W + j ) ] << " \n"[ j + 1 == W ];
}
}
}
signed main(){
ios::sync_with_stdio( 0 );
init();
preprocess();
solve();
return 0;
}