結果

問題 No.307 最近色塗る問題多くない?
ユーザー motimoti
提出日時 2015-11-28 03:13:13
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 4,320 bytes
コンパイル時間 1,250 ms
コンパイル使用メモリ 123,640 KB
実行使用メモリ 10,808 KB
最終ジャッジ日時 2023-10-12 02:28:29
合計ジャッジ時間 9,284 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,728 KB
testcase_01 AC 2 ms
4,448 KB
testcase_02 AC 2 ms
4,448 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,412 KB
testcase_07 AC 38 ms
4,540 KB
testcase_08 AC 647 ms
5,048 KB
testcase_09 AC 72 ms
4,736 KB
testcase_10 AC 15 ms
5,424 KB
testcase_11 AC 79 ms
10,452 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 39 ms
4,520 KB
testcase_14 AC 4 ms
4,516 KB
testcase_15 AC 5 ms
4,488 KB
testcase_16 AC 4 ms
4,572 KB
testcase_17 AC 15 ms
4,872 KB
testcase_18 AC 38 ms
5,424 KB
testcase_19 AC 54 ms
5,636 KB
testcase_20 AC 4 ms
4,428 KB
testcase_21 AC 53 ms
6,244 KB
testcase_22 AC 240 ms
6,376 KB
testcase_23 AC 236 ms
6,384 KB
testcase_24 AC 109 ms
6,260 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)
#define all(c) (c).begin(), (c).end()

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];
vector<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[chpos(i, j)]].push_back(uf[chpos(ni, nj)]);
      int rt = uf[chpos(i, j)];
      sort(all(outers[rt]));
      outers[rt].erase(unique(all(outers[rt])), outers[rt].end());
    }
  }

  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]]);
          copy(all(outers[uf[e]]), back_inserter(outers[RawPos]));
//          outers[uf[e]].clear();
          uf.unite(e, RawPos);
        }
      }
      if(RawPos != uf.root(RawPos)) {
  //      assert(outers[uf.root(RawPos)].empty());
        swap(outers[uf.root(RawPos)], outers[RawPos]);
      }
      sort(all(outers[uf[RawPos]]));
      outers[uf[RawPos]].erase(unique(all(outers[uf[RawPos]])), outers[uf[RawPos]].end());
      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