結果

問題 No.307 最近色塗る問題多くない?
ユーザー tentententen
提出日時 2020-12-23 18:25:43
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 3,848 bytes
コンパイル時間 2,611 ms
コンパイル使用メモリ 79,988 KB
実行使用メモリ 690,132 KB
最終ジャッジ日時 2023-10-21 15:15:14
合計ジャッジ時間 25,722 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 161 ms
57,004 KB
testcase_01 AC 128 ms
57,556 KB
testcase_02 AC 130 ms
57,548 KB
testcase_03 AC 125 ms
57,332 KB
testcase_04 AC 243 ms
59,992 KB
testcase_05 AC 140 ms
57,628 KB
testcase_06 AC 153 ms
57,636 KB
testcase_07 AC 338 ms
66,284 KB
testcase_08 AC 1,167 ms
181,984 KB
testcase_09 AC 827 ms
116,396 KB
testcase_10 AC 314 ms
64,152 KB
testcase_11 AC 500 ms
80,088 KB
testcase_12 AC 136 ms
57,292 KB
testcase_13 AC 532 ms
62,600 KB
testcase_14 AC 222 ms
61,200 KB
testcase_15 AC 256 ms
63,756 KB
testcase_16 AC 254 ms
63,228 KB
testcase_17 AC 802 ms
128,068 KB
testcase_18 MLE -
testcase_19 MLE -
testcase_20 AC 224 ms
60,548 KB
testcase_21 TLE -
testcase_22 TLE -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int w = sc.nextInt();
        boolean[][] field = new boolean[h][w];
        UnionFindTree uft = new UnionFindTree(h * w);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                field[i][j] = (sc.nextInt() == 1);
                uft.setColor(i * w + j, field[i][j]);
                if (j > 0) {
                    if (field[i][j] == field[i][j - 1]) {
                        uft.unite(i * w + j, i * w + j - 1);
                    }
                }
                if (i > 0) {
                    if (field[i][j] == field[i - 1][j]) {
                        uft.unite(i * w + j, (i - 1) * w + j);
                    }
                }
            }
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (j > 0) {
                    uft.addNeighbour(i * w + j, i * w + j - 1);
                    uft.addNeighbour(i * w + j - 1, i * w + j);
                }
                if (i > 0) {
                    uft.addNeighbour(i * w + j, (i - 1) * w + j);
                    uft.addNeighbour((i - 1) * w + j, i * w + j);
                }
            }
        }
        
        int q = sc.nextInt();
        for (int i = 0; i < q; i++) {
            int r = sc.nextInt() - 1;
            int c = sc.nextInt() - 1;
            boolean color = (sc.nextInt() == 1);
            uft.uniteColor(r * w + c, color);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (j > 0) {
                    sb.append(" ");
                }
                if (uft.getColor(i * w + j)) {
                    sb.append("1");
                } else {
                    sb.append("0");
                }
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
    
    static class UnionFindTree {
        int[] paretns;
        ArrayList<HashSet<Integer>> neighbours;
        boolean[] colors;
        
        public UnionFindTree(int size) {
            paretns = new int[size];
            neighbours = new ArrayList<>();
            colors = new boolean[size];
            for (int i = 0; i < size; i++) {
                paretns[i] = i;
                neighbours.add(new HashSet<>());
            }
        }
        
        public int find(int x) {
            if (x == paretns[x]) {
                return x;
            } else {
                return paretns[x] = find(paretns[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void addNeighbour(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return;
            }
            neighbours.get(xx).add(yy);
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return;
            }
            for (int zz : neighbours.get(xx)) {
                addNeighbour(yy, zz);
            }
            paretns[xx] = yy;
        }
        
        public void setColor(int x, boolean c) {
            colors[find(x)] = c;
        }
        
        public boolean getColor(int x) {
            return colors[find(x)];
        }
        
        public void uniteColor(int x, boolean c) {
            if (colors[find(x)] == c) {
                return;
            }
            colors[find(x)] = c;
            for (int y : neighbours.get(find(x))) {
                unite(x, y);
            }
        }
    }
}
0