結果

問題 No.307 最近色塗る問題多くない?
ユーザー T1610T1610
提出日時 2020-08-24 01:35:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,593 ms / 4,000 ms
コード長 3,676 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 211,160 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-04-23 18:47:39
合計ジャッジ時間 15,756 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 8 ms
5,376 KB
testcase_08 AC 31 ms
6,016 KB
testcase_09 AC 15 ms
5,376 KB
testcase_10 AC 7 ms
5,376 KB
testcase_11 AC 18 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 21 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 10 ms
5,376 KB
testcase_18 AC 16 ms
5,504 KB
testcase_19 AC 18 ms
6,144 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 18 ms
6,144 KB
testcase_22 AC 94 ms
6,272 KB
testcase_23 AC 17 ms
5,888 KB
testcase_24 AC 17 ms
6,016 KB
testcase_25 AC 39 ms
6,016 KB
testcase_26 AC 41 ms
6,016 KB
testcase_27 AC 71 ms
6,016 KB
testcase_28 AC 1,053 ms
6,400 KB
testcase_29 AC 15 ms
5,376 KB
testcase_30 AC 42 ms
6,272 KB
testcase_31 AC 105 ms
6,272 KB
testcase_32 AC 39 ms
6,144 KB
testcase_33 AC 3,498 ms
6,400 KB
testcase_34 AC 3,593 ms
6,528 KB
testcase_35 AC 3,532 ms
6,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}

// #define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\n'
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << " "
#define dumpVL(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x) 
#define dumpL(x) 
#define LINE 
#define LINEL 
#define dumpV(v) 
#define dumpVL(v) 
#define STOP assert(false)
#endif
#define mp make_pair
 
namespace std {
template<class S, class T>
ostream &operator <<(ostream& out, const pair<S, T>& a) {
    out << '(' << a.fi << ", " << a.se << ')';
    return out;
}
}

struct UF{ //O(loga(n))
    int n;
    int m;
    vi d, r;
    UF(int n) : n(n), m(n), d(n, -1), r(n, 0){};
    int root(int i){
        if(d[i] < 0) return i;
        return d[i] = root(d[i]);
    }
    bool same(int x, int y){
        return root(x) == root(y);
    }
    bool unite(int x, int y){
        x = root(x);
        y = root(y);
        if(x == y) return false;

        if(r[x] < r[y]) swap(x, y);
        else if(r[x] == r[y]) r[x]++;
        d[x] += d[y];
        d[y] = x;
        --m;
        return true;
    }
    int size(int i){
        return -d[root(i)];
    }
    int size() {
        return m;
    }
};

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

int main(){
    int h, w;
    cin >> h >> w;
    vector<vi> s(h, vi(w));
    rep(i, h) rep(j, w) cin >> s[i][j];
    const int n = h * w;
    vector<vi> nxts(n);
    UF uf(n);
    vi d(n);
    rep(y, h) rep(x, w) {
        int i = y * w + x;
        d[i] = s[y][x];
        rep(k, 4) {
            int nx = x + dx[k]; 
            int ny = y + dy[k]; 
            int j = ny * w + nx;
            if(nx < 0 || ny < 0 || ny >= h || nx >= w) continue;
            if(s[y][x] == s[ny][nx]) uf.unite(i, j);
            else nxts[i].emplace_back(j);
        }
    }
    rep(i, n) if(i != uf.root(i)) {
        int j = uf.root(i);
        for(auto&& nxt : nxts[i]) nxts[j].emplace_back(nxt);
        sort(all(nxts[j]));
        nxts[j].erase(unique(all(nxts[j])), nxts[j].end());
    }
    int q;
    cin >> q;
    rep(_, q) {
        int y, x, c;
        cin >> y >> x >> c;
        --y;
        --x;
        int i = y * w + x;
        i = uf.root(i);
        if(d[i] == c) continue;
        vi v;
        for(auto&& _nxt: nxts[i]) {
            int nxt = uf.root(_nxt);
            if(uf.same(i, nxt) || d[nxt] != c) continue;
            uf.unite(i, nxt);
            for(auto&& _j: nxts[nxt]) {
                int j = uf.root(_j);
                if(uf.same(i, j) || d[j] == c) continue;
                v.emplace_back(j);
            }
        }
        sort(all(v));
        v.erase(unique(all(v)), v.end());
        i = uf.root(i);
        nxts[i] = v;
        d[i] = c;
        dump(y); dump(x); dumpVL(v);
    }
    rep(y, h) rep(x, w) {
        int i = y * w + x;
        cout << d[uf.root(i)] << (x+1 == w ? '\n' : ' ');
    }
    return 0;
}
0