結果

問題 No.307 最近色塗る問題多くない?
ユーザー tentententen
提出日時 2023-05-02 08:54:32
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 5,062 bytes
コンパイル時間 2,828 ms
コンパイル使用メモリ 92,816 KB
実行使用メモリ 83,780 KB
最終ジャッジ日時 2024-05-01 01:38:45
合計ジャッジ時間 12,296 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
50,492 KB
testcase_01 AC 50 ms
50,144 KB
testcase_02 AC 48 ms
50,464 KB
testcase_03 AC 48 ms
50,304 KB
testcase_04 WA -
testcase_05 AC 48 ms
50,264 KB
testcase_06 AC 50 ms
50,584 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 140 ms
54,812 KB
testcase_11 AC 178 ms
56,872 KB
testcase_12 WA -
testcase_13 AC 182 ms
56,884 KB
testcase_14 AC 96 ms
52,340 KB
testcase_15 AC 115 ms
52,560 KB
testcase_16 WA -
testcase_17 AC 161 ms
56,392 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 101 ms
52,528 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 326 ms
76,740 KB
testcase_24 AC 325 ms
76,888 KB
testcase_25 AC 401 ms
77,320 KB
testcase_26 AC 427 ms
77,628 KB
testcase_27 AC 378 ms
83,780 KB
testcase_28 WA -
testcase_29 AC 179 ms
55,216 KB
testcase_30 AC 414 ms
78,080 KB
testcase_31 AC 478 ms
81,652 KB
testcase_32 AC 341 ms
78,108 KB
testcase_33 AC 178 ms
57,760 KB
testcase_34 AC 239 ms
57,768 KB
testcase_35 AC 228 ms
57,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        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.color[i * w + j] = field[i][j];
                if (j > 0 && field[i][j] == field[i][j - 1]) {
                    uft.unite(i * w + j, i * w + j - 1);
                }
                if (i > 0 && field[i][j] == field[i - 1][j]) {
                    uft.unite(i * w + j, (i - 1) * w + j);
                }
            }
        }
        uft.prepare();
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (j > 0 && field[i][j] != field[i][j - 1]) {
                    uft.next(i * w + j, i * w + j - 1);
                }
                if (i > 0 && field[i][j] != field[i - 1][j]) {
                    uft.next(i * w + j, (i - 1) * w + j);
                }
            }
        }
        int q = sc.nextInt();
        while (q-- > 0) {
            int idx = (sc.nextInt() - 1) * w + sc.nextInt() - 1;
            boolean isBlack = (sc.nextInt() == 1);
            if (uft.getColor(idx) == isBlack) {
                continue;
            }
            uft.setColor(idx, isBlack);
        }
        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[] parents;
        boolean[] color;
        HashMap<Integer, HashSet<Integer>> nextTo = new HashMap<>();
        
        public UnionFindTree(int size) {
            parents = new int[size];
            color = new boolean[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
        
        public boolean getColor(int x) {
            return color[find(x)];
        }
        
        public void prepare() {
            for (int i = 0; i < parents.length; i++) {
                if (i == find(i)) {
                    nextTo.put(i, new HashSet<>());
                }
            }
        }
        
        public void next(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            nextTo.get(xx).add(yy);
            nextTo.get(yy).add(xx);
        }
        
        public void setColor(int x, boolean c) {
            int xx = find(x);
            color[xx] = c;
            HashSet<Integer> newNext = new HashSet<>();
            for (int y : nextTo.get(xx)) {
                unite(y, xx);
                for (int z : nextTo.get(y)) {
                    if (find(z) != xx) {
                        newNext.add(find(z));
                    }
                }
            }
            nextTo.put(xx, newNext);
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0