結果

問題 No.307 最近色塗る問題多くない?
ユーザー tentententen
提出日時 2020-12-23 17:53:04
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,749 bytes
コンパイル時間 2,458 ms
コンパイル使用メモリ 80,276 KB
実行使用メモリ 90,624 KB
最終ジャッジ日時 2023-10-21 15:14:21
合計ジャッジ時間 20,938 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
57,336 KB
testcase_01 AC 137 ms
57,388 KB
testcase_02 AC 140 ms
57,500 KB
testcase_03 AC 143 ms
55,092 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 331 ms
62,116 KB
testcase_11 AC 450 ms
64,208 KB
testcase_12 WA -
testcase_13 AC 616 ms
64,160 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 236 ms
60,392 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 745 ms
82,356 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 939 ms
90,624 KB
testcase_32 AC 794 ms
84,724 KB
testcase_33 WA -
testcase_34 AC 825 ms
75,868 KB
testcase_35 AC 792 ms
78,100 KB
権限があれば一括ダウンロードができます

ソースコード

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);
                    } else {
                        uft.addNeighbour(i * w + j, i * w + j - 1);
                        uft.addNeighbour(i * w + j - 1, i * w + j);
                    }
                }
                if (i > 0) {
                    if (field[i][j] == field[i - 1][j]) {
                        uft.unite(i * w + j, (i - 1) * w + j);
                    } else {
                        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);
            if (uft.getColor(r * w + c) == color) {
                continue;
            }
            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, xx);
            }
            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) {
            HashSet<Integer> next = new HashSet<>();
            for (int y : neighbours.get(find(x))) {
                unite(x, y);
            }
        }
    }
}
0