結果

問題 No.307 最近色塗る問題多くない?
ユーザー 0w10w1
提出日時 2016-12-22 18:22:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,062 bytes
コンパイル時間 3,019 ms
コンパイル使用メモリ 204,760 KB
実行使用メモリ 17,748 KB
最終ジャッジ日時 2023-08-21 05:39:48
合計ジャッジ時間 12,308 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,600 KB
testcase_01 AC 3 ms
5,624 KB
testcase_02 AC 3 ms
5,592 KB
testcase_03 AC 4 ms
5,564 KB
testcase_04 AC 5 ms
5,700 KB
testcase_05 AC 3 ms
5,656 KB
testcase_06 AC 3 ms
5,572 KB
testcase_07 AC 47 ms
5,988 KB
testcase_08 AC 1,357 ms
7,376 KB
testcase_09 AC 95 ms
6,636 KB
testcase_10 AC 73 ms
7,584 KB
testcase_11 AC 511 ms
17,748 KB
testcase_12 AC 4 ms
5,620 KB
testcase_13 AC 27 ms
5,780 KB
testcase_14 AC 5 ms
5,772 KB
testcase_15 AC 5 ms
5,820 KB
testcase_16 AC 6 ms
5,864 KB
testcase_17 AC 20 ms
7,264 KB
testcase_18 AC 60 ms
10,128 KB
testcase_19 AC 63 ms
11,040 KB
testcase_20 AC 5 ms
5,980 KB
testcase_21 AC 116 ms
15,776 KB
testcase_22 AC 127 ms
15,356 KB
testcase_23 AC 308 ms
15,284 KB
testcase_24 AC 151 ms
15,680 KB
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
// #pragma GCC optimize ("O2") 以下で自動ベクトル化を行う場合は、#pragma GCC optimize ("tree-vectorize") も合わせて記述してください。
#pragma GCC target ("avx") // ターゲットの変更 sse4, avx, avx2 など
#pragma GCC optimize ("fast-math")

#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;
  vi size;
  dsu( int sz ){
    fa = size = vi( sz );
    for( int i = 0; i < sz; ++i ){
      fa[ i ] = i;
      size[ i ] = 1;
    }
  }
  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;
    // if( not ( size[ a ] >= size[ b ] ) )
      // swap( a, b );
    // size[ a ] += size[ b ];
    fa[ b ] = a;
    return 1;
  }
} *ccid_uf;

unordered_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 ) == ccid_uf->find( 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;
    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 );
    }
    cc_col[ ccid_uf->find( R * W + C ) ] = X;
  }
  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;
}
0