結果

問題 No.307 最近色塗る問題多くない?
ユーザー parukiparuki
提出日時 2016-07-08 14:45:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,598 bytes
コンパイル時間 1,862 ms
コンパイル使用メモリ 175,584 KB
実行使用メモリ 13,696 KB
最終ジャッジ日時 2024-04-21 01:05:57
合計ジャッジ時間 4,316 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,760 KB
testcase_01 AC 3 ms
5,632 KB
testcase_02 AC 3 ms
5,760 KB
testcase_03 AC 3 ms
5,632 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 7 ms
6,528 KB
testcase_11 AC 12 ms
7,424 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 4 ms
6,016 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 34 ms
13,440 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 40 ms
13,440 KB
testcase_32 AC 34 ms
13,568 KB
testcase_33 WA -
testcase_34 AC 29 ms
9,728 KB
testcase_35 AC 28 ms
9,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

class UnionFind{
public:
    UnionFind(int _n):n(_n), cnt(_n), par(_n), rank(_n), size(_n, 1){
        for(int i=0;i<n;++i) par[i]=i;
    }
    int find(int k){
        return (k==par[k])?k:(par[k]=find(par[k]));
    }
    int operator[](int k){
        return find(k);
    }
    int getSize(int k){
        return size[find(k)];
    }
    void unite(int x, int y){
        x = find(x); y = find(y);
        if(x==y) return;
        --cnt;
        if(rank[x] < rank[y]){
            par[x] = y;
            size[y] += size[x];
        }else{
            par[y] = x;
            size[x] += size[y];
            if(rank[y] == rank[x]) ++rank[y];
        }
    }
    int count(){
        return cnt;
    }
private:
    int n, cnt;
    vector<int> par, rank, size;
};

const int DY[] = {-1,0,1,0};
const int DX[] = {0,1,0,-1};

int A[200][200], col[200*200];
set<int> G[200*200];

int main(){
    int H, W;
    while(cin >> H >> W){
        rep(y, H)rep(x, W)scanf("%d", &A[y][x]);
        rep(i, H*W)G[i].clear();

        UnionFind uf(H*W);
        rep(y, H)rep(x, W){
            int id = y*W + x;
            col[id] = A[y][x];
            rep(i, 4){
                int ny = y + DY[i], nx = x + DX[i];
                if(ny >= 0 && nx >= 0 && ny < H&&nx < W){
                    int nid = ny*W + nx;
                    if(A[y][x] == A[ny][nx])uf.unite(id, nid);
                    else G[id].insert(nid);
                }
            }
        }
        int Q; cin >> Q;
        while(Q--){
            int Y, X, C;
            scanf("%d%d%d", &Y, &X, &C);
            --Y;
            --X;
            int id = uf.find(Y*W + X);
            if(col[id] == C)continue;
            col[id] = C;
            each(to, G[id]){
                uf.unite(id, to);
                int idid = uf[id];
                col[idid] = C;
                G[to].erase(id);
            }
            G[id].clear();
        }
        rep(y, H){
            rep(x, W){
                int id = uf[y*W + x];
                printf("%d%c", col[id], x == W - 1 ? '\n' : ' ');
            }
        }
    }
}
0