結果

問題 No.307 最近色塗る問題多くない?
ユーザー uafr_csuafr_cs
提出日時 2015-11-28 00:16:00
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,516 bytes
コンパイル時間 2,452 ms
コンパイル使用メモリ 81,996 KB
実行使用メモリ 615,716 KB
最終ジャッジ日時 2023-10-12 00:37:23
合計ジャッジ時間 30,923 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,692 KB
testcase_01 AC 133 ms
55,812 KB
testcase_02 AC 137 ms
55,912 KB
testcase_03 AC 134 ms
55,664 KB
testcase_04 AC 308 ms
59,256 KB
testcase_05 AC 164 ms
55,780 KB
testcase_06 AC 201 ms
56,164 KB
testcase_07 AC 550 ms
68,920 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 563 ms
75,300 KB
testcase_11 AC 969 ms
90,928 KB
testcase_12 WA -
testcase_13 AC 567 ms
62,288 KB
testcase_14 AC 380 ms
63,384 KB
testcase_15 AC 456 ms
67,620 KB
testcase_16 WA -
testcase_17 AC 1,210 ms
155,060 KB
testcase_18 MLE -
testcase_19 MLE -
testcase_20 AC 384 ms
64,948 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.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();
		}
		
	}

}
0