結果
問題 | No.307 最近色塗る問題多くない? |
ユーザー | uafr_cs |
提出日時 | 2015-11-28 00:16:00 |
言語 | Java21 (openjdk 21) |
結果 |
MLE
|
実行時間 | - |
コード長 | 4,516 bytes |
コンパイル時間 | 2,403 ms |
コンパイル使用メモリ | 87,432 KB |
実行使用メモリ | 683,936 KB |
最終ジャッジ日時 | 2024-09-14 00:31:00 |
合計ジャッジ時間 | 27,080 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | MLE | - |
testcase_01 | AC | 126 ms
40,216 KB |
testcase_02 | AC | 143 ms
41,400 KB |
testcase_03 | AC | 140 ms
41,232 KB |
testcase_04 | AC | 306 ms
46,844 KB |
testcase_05 | AC | 163 ms
41,696 KB |
testcase_06 | AC | 206 ms
41,968 KB |
testcase_07 | AC | 589 ms
57,488 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 608 ms
61,380 KB |
testcase_11 | AC | 1,019 ms
77,124 KB |
testcase_12 | WA | - |
testcase_13 | AC | 650 ms
49,144 KB |
testcase_14 | AC | 387 ms
51,188 KB |
testcase_15 | AC | 471 ms
55,380 KB |
testcase_16 | WA | - |
testcase_17 | AC | 1,239 ms
139,564 KB |
testcase_18 | MLE | - |
testcase_19 | TLE | - |
testcase_20 | AC | 396 ms
63,240 KB |
testcase_21 | MLE | - |
testcase_22 | -- | - |
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 | -- | - |
ソースコード
import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; public class Main { public static final int[][] nexts = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; public static boolean is_in(int x, int y, int W, int H){ return 0 <= x && x < W && 0 <= y && y < H; } public static class UnionFind{ int[] par; // public UnionFind(int n){ par = new int[n]; for(int i = 0; i < n; i++){ par[i] = -1; } } public int find(int x){ if(par[x] < 0){ return x; }else{ return par[x] = find(par[x]); } } public boolean union(int x, int y){ x = find(x); y = find(y); if(x != y){ if(par[y] < par[x]) { // 多い方が根になるようにスワップする. int tmp = x; x = y; y = tmp; } par[x] += par[y]; par[y] = x; return true; }else{ return false; } } public boolean same(int x, int y){ return find(x) == find(y); } public int size(int x){ return -par[find(x)]; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int H = sc.nextInt(); final int W = sc.nextInt(); boolean[][] colors = new boolean[H][W]; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ colors[i][j] = sc.nextInt() == 1; } } UnionFind uf = new UnionFind(H * W); ArrayList<HashSet<Integer>> adjs = new ArrayList<HashSet<Integer>>(H * W); for(int y = 0; y < H; y++){ for(int x = 0; x < W; x++){ final HashSet<Integer> set = new HashSet<Integer>(); final int id = y * W + x; final int uf_id = uf.find(id); for(int[] next : nexts){ final int nx = x + next[0]; final int ny = y + next[1]; if(!is_in(nx, ny, W, H)){ continue; }else if(colors[ny][nx] == colors[y][x]){ //uf.union(ny, nx); continue; } final int next_id = ny * W + nx; set.add(next_id); } adjs.add(set); } } for(int y = 0; y < H; y++){ for(int x = 0; x < W; x++){ final int id = y * W + x; final int uf_id = uf.find(id); for(int[] next : nexts){ final int nx = x + next[0]; final int ny = y + next[1]; if(!is_in(nx, ny, W, H)){ continue; } final int next_id = ny * W + nx; final int next_uf_id = uf.find(next_id); if(colors[next_uf_id / W][next_uf_id % W] == colors[uf_id / W][uf_id % W]){ HashSet<Integer> merged = new HashSet<Integer>(); for(final int uf_ids : adjs.get(uf_id)){ if(uf.find(uf_ids) != uf.find(next_uf_id)){ merged.add(uf.find(uf_ids)); } } for(final int next_uf_ids : adjs.get(next_uf_id)){ if(uf.find(next_uf_ids) != uf.find(uf_id)){ merged.add(uf.find(next_uf_ids)); } } uf.union(uf_id, next_uf_id); adjs.set(uf_id, merged); adjs.set(next_uf_id, merged); } } } } final int Q = sc.nextInt(); for(int i = 0; i < Q; i++){ final int y = sc.nextInt() - 1; final int x = sc.nextInt() - 1; final boolean color = sc.nextInt() == 1; final int id = y * W + x; final int uf_id = uf.find(id); if(colors[uf_id / W][uf_id % W] == color){ continue; } colors[uf_id / W][uf_id % W] = color; //System.out.println(i + " orig : " + (uf_id / W) + " " + (uf_id % W)); for(final int next_uf_id : adjs.get(uf_id)){ //System.out.println(i + " : " + (next_uf_id / W) + " " + (next_uf_id % W)); HashSet<Integer> merged = new HashSet<Integer>(); for(final int uf_ids : adjs.get(uf_id)){ if(uf.find(uf_ids) != uf.find(next_uf_id)){ merged.add(uf.find(uf_ids)); } } for(final int next_uf_ids : adjs.get(next_uf_id)){ if(uf.find(next_uf_ids) != uf.find(uf_id)){ merged.add(uf.find(next_uf_ids)); } } uf.union(uf_id, next_uf_id); adjs.set(uf_id, merged); adjs.set(next_uf_id, merged); } } for(int y = 0; y < H; y++){ for(int x = 0; x < W; x++){ final int uf_id = uf.find(y * W + x); if(x != 0){ System.out.printf(" "); } System.out.print(colors[uf_id / W][uf_id % W] ? 1 : 0); } System.out.println(); } } }