結果
問題 | No.307 最近色塗る問題多くない? |
ユーザー | tenten |
提出日時 | 2023-05-02 08:54:32 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,062 bytes |
コンパイル時間 | 2,703 ms |
コンパイル使用メモリ | 94,396 KB |
実行使用メモリ | 83,420 KB |
最終ジャッジ日時 | 2024-11-21 00:52:39 |
合計ジャッジ時間 | 13,864 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
50,476 KB |
testcase_01 | AC | 54 ms
50,456 KB |
testcase_02 | AC | 54 ms
50,444 KB |
testcase_03 | AC | 53 ms
50,552 KB |
testcase_04 | WA | - |
testcase_05 | AC | 55 ms
50,444 KB |
testcase_06 | AC | 55 ms
50,404 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 166 ms
54,640 KB |
testcase_11 | AC | 214 ms
56,300 KB |
testcase_12 | WA | - |
testcase_13 | AC | 211 ms
56,452 KB |
testcase_14 | AC | 117 ms
52,240 KB |
testcase_15 | AC | 133 ms
52,484 KB |
testcase_16 | WA | - |
testcase_17 | AC | 194 ms
57,400 KB |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | AC | 116 ms
52,344 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | AC | 387 ms
76,644 KB |
testcase_24 | AC | 392 ms
76,736 KB |
testcase_25 | AC | 473 ms
77,352 KB |
testcase_26 | AC | 497 ms
77,820 KB |
testcase_27 | AC | 446 ms
83,420 KB |
testcase_28 | WA | - |
testcase_29 | AC | 194 ms
55,304 KB |
testcase_30 | AC | 484 ms
78,792 KB |
testcase_31 | AC | 533 ms
82,864 KB |
testcase_32 | AC | 399 ms
78,204 KB |
testcase_33 | AC | 198 ms
57,664 KB |
testcase_34 | AC | 269 ms
57,680 KB |
testcase_35 | AC | 275 ms
57,660 KB |
ソースコード
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(); } }