結果

問題 No.307 最近色塗る問題多くない?
ユーザー nanophoto12nanophoto12
提出日時 2016-01-01 22:07:51
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,240 bytes
コンパイル時間 981 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 45,140 KB
最終ジャッジ日時 2023-10-19 13:19:14
合計ジャッジ時間 3,603 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 19 ms
43,012 KB
testcase_03 AC 19 ms
43,012 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
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 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 50 ms
44,744 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 55 ms
45,080 KB
testcase_28 WA -
testcase_29 AC 34 ms
43,012 KB
testcase_30 AC 76 ms
44,816 KB
testcase_31 WA -
testcase_32 AC 62 ms
44,552 KB
testcase_33 AC 39 ms
45,140 KB
testcase_34 AC 66 ms
45,140 KB
testcase_35 AC 67 ms
45,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <cmath>

using namespace std;

#define FOR(x,y) for(int x = 0;x < y;x++)
#define LLI long long int
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())

template<int um> class UF {
public:
    vector<int> _parent,_rank;
    UF()
    {
        _parent=_rank=vector<int>(um,0);
        for(int i=0;i<um;i++)
        {
            _parent[i]=i;
        }
    }
    
    int Find(int x)
    {
        if(_parent[x] == x)
        {
            return x;
        }
        _parent[x] = Find(_parent[x]);
        return _parent[x];
    }
    
    int Union(int x,int y)
    {
        int xRoot = Find(x);
        int yRoot = Find(y);
        if(_rank[xRoot]>_rank[yRoot])
        {
            _parent[xRoot] = yRoot;
           return yRoot;
        }
        if(_rank[xRoot]<_rank[yRoot])
        {
            _parent[yRoot] = xRoot;
            return xRoot;
        }
        if(xRoot != yRoot)
        {
            _parent[yRoot] = xRoot;
            _rank[xRoot]++;
            return xRoot;
        }
        return xRoot;
    }
};

int H,W,Q;
int Board[201*201];
vector<int> S[201*201];
int GetIndex(int y,int x)
{
    return y*W+x;
}

UF<5000000> uf;

int main() {
    cin >> H >> W;
    FOR(y, H)
    {
        FOR(x, W)
        {
            cin >> Board[GetIndex(y, x)];
        }
    }
    
    
    FOR(y,H) FOR(x,W) {
        if(y) {
            if(Board[GetIndex(y,x)]==Board[GetIndex(y-1,x)]) uf.Union(GetIndex(y,x),GetIndex(y-1,x));
            else S[GetIndex(y,x)].push_back(GetIndex(y-1,x));
        }
        if(y<H-1) {
            if(Board[GetIndex(y,x)]==Board[GetIndex(y+1,x)]) uf.Union(GetIndex(y,x),GetIndex(y+1,x));
            else S[GetIndex(y,x)].push_back(GetIndex(y+1,x));
        }
        if(x) {
            if(Board[GetIndex(y,x)]==Board[GetIndex(y,x-1)]) uf.Union(GetIndex(y,x),GetIndex(y,x-1));
            else S[GetIndex(y,x)].push_back(GetIndex(y,x-1));
        }
        if(x<W-1) {
            if(Board[GetIndex(y,x)]==Board[GetIndex(y,x+1)]) uf.Union(GetIndex(y,x),GetIndex(y,x+1));
            else S[GetIndex(y,x)].push_back(GetIndex(y,x+1));
        }
    }
    
    FOR(x,H*W)
    {
      if(uf.Find(x)!=x)
      {
        FORR(r,S[x]) S[uf.Find(x)].push_back(r);
      }
    }
    FOR(x,H*W)
    {
        if(uf.Find(x)==x)
        {
            sort(ALL(S[x]));
            S[x].erase(unique(ALL(S[x])),S[x].end());
        }
    }
    cin>>Q;
    FOR(i,Q) {
        int y, x, k;
        cin>>y>>x>>k;
        int parent =uf.Find(GetIndex(y-1,x-1));
        if(Board[parent]==k) continue;
        
        vector<int> NEW;
        FORR(r,S[parent])
        {
          if(uf.Find(parent)!=uf.Find(r))
          {
              FORR(r2,S[uf.Find(r)]) if(uf.Find(r2)!=uf.Find(parent)) NEW.push_back(uf.Find(r2));
              uf.Union(x,r);
          }
        }
        sort(ALL(NEW));
        NEW.erase(unique(ALL(NEW)),NEW.end());
        
        S[uf.Find(parent)]=NEW;
        Board[uf.Find(parent)]=k;
    }
    
    FOR(y,H) FOR(x,W) printf("%d%c",Board[uf.Find(GetIndex(y,x))],(x==W-1)?'\n':' ');
    return 0;
}
0