結果

問題 No.307 最近色塗る問題多くない?
ユーザー motimoti
提出日時 2015-11-28 02:55:25
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,921 bytes
コンパイル時間 1,161 ms
コンパイル使用メモリ 118,060 KB
実行使用メモリ 17,304 KB
最終ジャッジ日時 2023-10-12 02:25:01
合計ジャッジ時間 8,957 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,508 KB
testcase_01 AC 3 ms
5,260 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,252 KB
testcase_04 AC 4 ms
5,348 KB
testcase_05 AC 3 ms
5,244 KB
testcase_06 AC 3 ms
5,268 KB
testcase_07 AC 34 ms
5,528 KB
testcase_08 AC 628 ms
6,348 KB
testcase_09 AC 64 ms
5,944 KB
testcase_10 AC 7 ms
5,652 KB
testcase_11 AC 15 ms
5,812 KB
testcase_12 AC 3 ms
5,236 KB
testcase_13 AC 36 ms
5,388 KB
testcase_14 AC 5 ms
5,568 KB
testcase_15 AC 6 ms
5,456 KB
testcase_16 AC 5 ms
5,688 KB
testcase_17 AC 17 ms
6,684 KB
testcase_18 AC 48 ms
8,940 KB
testcase_19 AC 51 ms
9,396 KB
testcase_20 AC 4 ms
5,664 KB
testcase_21 AC 58 ms
12,608 KB
testcase_22 AC 59 ms
12,484 KB
testcase_23 AC 260 ms
12,752 KB
testcase_24 AC 119 ms
12,760 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 #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

struct UnionFind
{
  vector<int> rank;
  vector<int> rid;

  UnionFind(int n) {
    rank.resize(n);
    rid.assign(n, -1);
  }

  void unite(int u, int v) {
    u = root(u), v = root(v);
    if(u == v) { return ; }
    if(rank[u] < rank[v]) {
      rid[u] = v;
    }
    else {
      rid[v] = u;
      if(rank[u] == rank[v]) {
        rank[u]++;
      }
    }
  }

  bool same(int u, int v) {
    return root(u) == root(v);
  }

  inline int root(int x) {
    if(rid[x] < 0) return x;
    else return rid[x] = root(rid[x]);
  }

  inline int operator[](int x) { return root(x); }
};

int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};

int H, W;
int A[201][201];
int Q;
bool rootCol[200*200+1];
set<int> outers[200*200+1]; // []はroot

bool inrange(int y, int x) {
  return 0<=y&&y<H && 0<=x&&x<W;
}

typedef pair<int, int> pii;

inline int chpos(int y, int x) { return y*W+x; }
inline pii coord(int p) { return std::move(make_pair(p/W, p%W)); }

#define DEBUG debug(uf);
void debug(UnionFind& uf) {
cout << "v check ----------\n";
  rep(i, H) {
    rep(j, W) {
      if(j) cout << " ";
      cout << rootCol[uf.root(chpos(i, j))];
    }
    cout << endl;
  }
cout << "^ check ----------\n";
}

void merge_set(set<int>& a, set<int>& b) {
  if (a.size() < b.size()) swap(a, b);
  if(b.empty()) { return; }
  a.insert(b.begin(), b.end());
  b.clear();
}

int main() {

  cin >> H >> W;

  rep(i, H) rep(j, W) {
    cin >> A[i][j];
    rootCol[chpos(i, j)] = A[i][j];
  }

  UnionFind uf(H * W + 1);

  rep(i, H) rep(j, W) rep(k, 4) {
    int ni = i+dy[k], nj = j+dx[k];
    if(!inrange(ni, nj)) { continue; }
    if(A[i][j] == A[ni][nj]) {
      uf.unite(chpos(i, j), chpos(ni, nj));
      rootCol[uf.root(chpos(i, j))] = A[i][j];
    }
  }

  rep(i, H) rep(j, W) rep(k, 4) {
    int ni = i+dy[k], nj = j+dx[k];
    if(!inrange(ni, nj)) { continue; }
    if(A[i][j] != A[ni][nj]) {
      outers[uf.root(chpos(i, j))].insert(uf[chpos(ni, nj)]);
    }
  }

  cin >> Q;
  rep(_, Q) {
    int r, c, x;
    cin >> r >> c >> x; r--, c--;
//    #define RawPos uf.root(chpos(r, c))
    int const RawPos = uf.root(chpos(r, c));
/*
    cout << "rc outers " << r+1 << ", " << c+1 << endl;
    for(auto& e: outers[RawPos]) {
      cout << coord(e).first + 1 << ", " << coord(e).second + 1 << endl;
    }cout << endl;
//*/
    if(rootCol[RawPos] != x) {
//      refCol = x;
      vector<int> os; for(auto& e: outers[RawPos]) os.push_back(e);
      for(auto& e: os) {
        if(uf.same(e, RawPos)) { continue; }
//        if(e != uf[e]) { assert(outers[e].empty()); }
        if(rootCol[uf[e]] != rootCol[RawPos]) {
          merge_set(outers[RawPos], outers[uf[e]]);
          uf.unite(e, RawPos);
        }
      }
      if(RawPos != uf.root(RawPos)) {
  //      assert(outers[uf.root(RawPos)].empty());
        swap(outers[uf.root(RawPos)], outers[RawPos]);
      }
      rootCol[uf.root(RawPos)] = x;
    }

//    cout << "query: (" << r+1 << ", " << c+1 << ") " << x << "\n";
//    DEBUG


  /*
      cout << "rc outers " << r+1 << ", " << c+1 << " (" << coord(uf[RawPos]).first << ", " << coord(uf[RawPos]).second << ")\n";
      for(auto& e: outers[uf[RawPos]]) {
        cout << coord(e).first + 1 << ", " << coord(e).second + 1 << endl;
      }cout << endl << "------------------------------\n\n";
  //*/
  }

  rep(i, H) {
    rep(j, W) {
      if(j) cout << " ";
      cout << rootCol[uf.root(chpos(i, j))];
    }
    cout << endl;
  }


  return 0;
}
0