結果

問題 No.307 最近色塗る問題多くない?
ユーザー minatominato
提出日時 2020-08-25 23:02:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,936 bytes
コンパイル時間 3,144 ms
コンパイル使用メモリ 240,136 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-06 08:57:08
合計ジャッジ時間 12,556 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 WA -
testcase_08 AC 149 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 89 ms
4,376 KB
testcase_11 AC 555 ms
4,376 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 32 ms
4,376 KB
testcase_18 AC 102 ms
4,376 KB
testcase_19 AC 100 ms
4,376 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 150 ms
4,376 KB
testcase_22 AC 43 ms
4,380 KB
testcase_23 AC 361 ms
4,380 KB
testcase_24 AC 342 ms
4,380 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 TLE -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln = '\n';
//constexpr long long MOD = 1000000007;
constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct UnionFind {
    int N;
    vector<int> node;

    UnionFind(){}
    UnionFind(int N) : N(N), node(N,-1) {}

    void init(int x) {
        node.assign(x,-1);
        N = x;
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (node[x] > node[y]) swap(x,y);
        node[x] += node[y];
        node[y] = x;
        N--;
        return true; 
    }

    bool same(int x, int y) {return root(x) == root(y);}

    int root(int x) {
        if (node[x] < 0) return x;
        return node[x] = root(node[x]);
    }

    int size(int x) {return -node[root(x)];}

    int count() const {return N;}
};

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

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int H,W; cin >> H >> W;
    vector<vector<int>> A(H, vector<int>(W));
    rep(i,H) rep(j,W) cin >> A[i][j];

    UnionFind uf(H*W);
    auto mer=[&]() {
        rep(x,H) rep(y,W) {
            rep(k,4) {
                int nx = x + dx[k];
                int ny = y + dy[k];
                if (nx < 0 or nx >= H or ny < 0 or ny >= W) continue;
                if (A[x][y]==A[nx][ny]) uf.merge(x*W+y,nx*W+ny); 
            }
        }
    };

    mer();
    int Q; cin >> Q;
    int all = -1;
    while (Q--) {
        int h,w,c; cin >> h >> w >> c;
        --h; --w;
        if (A[h][w]==c) continue;
        if (uf.size(h*W+w)==H*W) {
            all = c;
            continue;
        }
        rep(x,H) rep(y,W) {
            if (uf.same(x*W+y,h*W+w)) A[x][y] = c; 
        }
        mer();  
    }

    if (~all) {
        rep(i,H) {
            rep(j,W) cout << all << " ";
            cout << ln;
        }
    } else {
        rep(i,H) {
            rep(j,W) cout << A[i][j] << " ";
            cout << ln;
        }
    }
}
0