結果

問題 No.307 最近色塗る問題多くない?
ユーザー kimiyukikimiyuki
提出日時 2016-03-01 21:46:35
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,187 bytes
コンパイル時間 786 ms
コンパイル使用メモリ 76,704 KB
実行使用メモリ 15,612 KB
最終ジャッジ日時 2023-10-24 19:22:39
合計ジャッジ時間 10,499 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 7 ms
4,348 KB
testcase_11 AC 16 ms
5,580 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 3 ms
4,348 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 1,936 ms
15,612 KB
testcase_24 AC 766 ms
15,612 KB
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
typedef long long ll;
using namespace std;
struct union_find {
    vector<int> tree;
    explicit union_find(size_t n) : tree(n, -1) {}
    bool is_root(int a) { return tree[a] < 0; }
    int find_root(int a) { return is_root(a) ? a : (tree[a] = find_root(tree[a])); }
    int tree_size(int a) { return - tree[find_root(a)]; }
    void union_tree(int a, int b) {
        a = find_root(a); b = find_root(b);
        if (a != b) {
            if (not (tree_size(a) < tree_size(b))) swap(a,b);
            tree[b] += tree[a];
            tree[a] = b;
        }
    }
    bool is_connected(int a, int b) { return find_root(a) == find_root(b); }
};
int main() {
    int h, w; cin >> h >> w;
    auto ix = [&](int y, int x) { return y*w + x; };
    vector<int> a(h * w); repeat (y,h) repeat (x,w) cin >> a[ix(y,x)];
    union_find t(h * w);
    repeat (y,h) repeat (x,w) {
        if (y+1 < h and a[ix(y,x)] == a[ix(y+1,x)]) t.union_tree(ix(y,x), ix(y+1,x));
        if (x+1 < w and a[ix(y,x)] == a[ix(y,x+1)]) t.union_tree(ix(y,x), ix(y,x+1));
    }
    vector<set<int> > g(h * w);
    repeat (y,h) repeat (x,w) {
        if (y+1 < h and a[ix(y,x)] != a[ix(y+1,x)]) {
            g[ix(y,x)].insert(ix(y+1,x));
            g[ix(y+1,x)].insert(ix(y,x));
        }
        if (x+1 < w and a[ix(y,x)] != a[ix(y,x+1)]) {
            g[ix(y,x)].insert(ix(y,x+1));
            g[ix(y,x+1)].insert(ix(y,x));
        }
    }
    int q; cin >> q;
    repeat (query,q) {
        int y, x, p; cin >> y >> x >> p; -- y; -- x;
        int i = t.find_root(ix(y,x));
        if (a[i] == p) continue;
        a[i] = p;
        set<int> s;
        for (int j : g[i]) {
            t.union_tree(i,j);
            for (int k : g[j]) s.insert(k);
        }
        for (int j : g[i]) s.erase(j);
        s.erase(i);
        int k = t.find_root(i);
        g[k] = s;
        for (int j : g[k]) g[j].insert(k);
        a[k] = p;
    }
    repeat (y,h) {
        repeat (x,w) {
            if (x) cout << ' ';
            cout << a[t.find_root(ix(y,x))];
        }
        cout << endl;
    }
    return 0;
}
0