結果

問題 No.307 最近色塗る問題多くない?
ユーザー imulanimulan
提出日時 2016-10-02 23:41:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 4,000 ms
コード長 3,733 bytes
コンパイル時間 2,124 ms
コンパイル使用メモリ 187,596 KB
実行使用メモリ 13,356 KB
最終ジャッジ日時 2023-08-14 04:13:27
合計ジャッジ時間 4,612 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,512 KB
testcase_01 AC 3 ms
5,508 KB
testcase_02 AC 3 ms
5,836 KB
testcase_03 AC 3 ms
5,620 KB
testcase_04 AC 4 ms
5,648 KB
testcase_05 AC 3 ms
5,652 KB
testcase_06 AC 3 ms
5,640 KB
testcase_07 AC 8 ms
5,804 KB
testcase_08 AC 27 ms
6,504 KB
testcase_09 AC 14 ms
6,076 KB
testcase_10 AC 7 ms
5,884 KB
testcase_11 AC 13 ms
6,060 KB
testcase_12 AC 3 ms
5,584 KB
testcase_13 AC 16 ms
5,664 KB
testcase_14 AC 4 ms
5,612 KB
testcase_15 AC 5 ms
5,800 KB
testcase_16 AC 6 ms
5,716 KB
testcase_17 AC 16 ms
6,692 KB
testcase_18 AC 34 ms
8,980 KB
testcase_19 AC 37 ms
9,676 KB
testcase_20 AC 5 ms
5,880 KB
testcase_21 AC 47 ms
12,792 KB
testcase_22 AC 59 ms
12,452 KB
testcase_23 AC 51 ms
12,764 KB
testcase_24 AC 54 ms
12,980 KB
testcase_25 AC 69 ms
12,936 KB
testcase_26 AC 70 ms
13,268 KB
testcase_27 AC 53 ms
13,256 KB
testcase_28 AC 84 ms
13,356 KB
testcase_29 AC 12 ms
5,568 KB
testcase_30 AC 63 ms
13,212 KB
testcase_31 AC 57 ms
13,356 KB
testcase_32 AC 38 ms
13,296 KB
testcase_33 AC 12 ms
5,636 KB
testcase_34 AC 22 ms
5,804 KB
testcase_35 AC 22 ms
5,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pi;

struct UF{
    // Graph
    set<int> G[40000];

    int n;
    //正だったらその頂点の親,負だったら根で連結成分の個数
    vector<int> d;
    UF() {}
    UF(int N):n(N), d(N,-1){}
    int root(int v){
        if(d[v]<0) return v;
        return d[v]=root(d[v]);
    }
    bool unite(int X,int Y){
        X=root(X); Y=root(Y);
        if(X==Y) return false;
        if(size(X) < size(Y)) swap(X,Y);
        d[X]+=d[Y];
        d[Y]=X;

        G[X].insert(G[Y].begin(), G[Y].end());
        G[Y].clear();

        return true;
    }
    int size(int v){ return -d[root(v)]; }
};

int h,w;
int a[200][200];

// connected component
int cc[200][200];
int dx[4]={1,-1,0,0}, dy[4]={0,0,1,-1};
int col[40000];

int main()
{
    scanf(" %d %d", &h, &w);
    rep(i,h)rep(j,w) scanf(" %d", &a[i][j]);

    // BFS
    #define IN(x,y) (0<=x && x<w && 0<=y && y<h)
    memset(cc,-1,sizeof(cc));
    int cc_num=0;
    rep(y,h)rep(x,w)
    {
        if(cc[y][x]>=0) continue;

        queue<pi> que;
        que.push(pi(y,x));
        cc[y][x]=cc_num;
        while(!que.empty())
        {
            pi now=que.front();
            que.pop();
            rep(i,4)
            {
                int nx=now.se+dx[i], ny=now.fi+dy[i];
                if(IN(nx,ny) && a[ny][nx]==a[now.fi][now.se] && cc[ny][nx]==-1)
                {
                    cc[ny][nx]=cc_num;
                    que.push(pi(ny,nx));
                }
            }
        }

        ++cc_num;
    }

    UF uf(cc_num);

    // グラフ構成
    rep(y,h)rep(x,w)
    {
        col[cc[y][x]] = a[y][x];
        rep(i,4)
        {
            int nx=x+dx[i], ny=y+dy[i];
            if(IN(nx,ny) && cc[y][x]!=cc[ny][nx])
            {
                int p=cc[y][x], q=cc[ny][nx];
                uf.G[p].insert(q);
                uf.G[q].insert(p);
            }
        }
    }

    // rep(i,h)
    // {
    //     rep(j,w) printf("%3d", cc[i][j]);
    //     printf("\n");
    // }
    // rep(i,cc_num)
    // {
    //     printf(" %d(col=%d) :", i,col[i]);
    //     for(const int &j:G[i]) printf(" %d", j);
    //     printf("\n");
    // }

    // クエリ処理
    int Q;
    scanf(" %d", &Q);
    while(Q--)
    {
        int r,c,x;
        scanf(" %d %d %d", &r, &c, &x);
        --r;
        --c;

        if(x == col[ uf.root(cc[r][c]) ]) continue;

        vector<int> tmp(uf.G[uf.root(cc[r][c])].begin(), uf.G[uf.root(cc[r][c])].end());

        for(auto &i:tmp) i=uf.root(i);
        sort(all(tmp));
        tmp.erase(unique(all(tmp)), tmp.end());

        uf.G[uf.root(cc[r][c])].clear();
        uf.G[uf.root(cc[r][c])].insert(all(tmp));

        set<int> tmpG=uf.G[uf.root(cc[r][c])];
        for(const int &nx:tmpG)
        {
            if(col[uf.root(nx)]==x)
            {
                uf.unite(uf.root(cc[r][c]),uf.root(nx));
            }
        }

        col[uf.root(cc[r][c])] = x;

        // printf(" process: %d\n",Q);
        // rep(i,cc_num)
        // {
        //     printf(" %d(col=%d) :", i,col[i]);
        //     for(const int &j:G[i]) printf(" %d", j);
        //     printf("\n");
        // }
    }

    // rep(y,h)rep(x,w) printf("%d,%d -> root: %d\n", y,x,uf.root(cc[y][x]) );

    // 出力
    rep(y,h)
    {
        rep(x,w)
        {
            if(x) printf(" ");
            printf("%d", col[uf.root(cc[y][x])]);
        }
        printf("\n");
    }
    return 0;
}
0