結果

問題 No.307 最近色塗る問題多くない?
ユーザー uafr_csuafr_cs
提出日時 2015-11-28 00:21:20
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,991 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 79,868 KB
実行使用メモリ 659,568 KB
最終ジャッジ日時 2024-09-14 00:35:03
合計ジャッジ時間 29,977 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
54,336 KB
testcase_01 AC 139 ms
53,992 KB
testcase_02 AC 142 ms
54,076 KB
testcase_03 AC 144 ms
54,116 KB
testcase_04 WA -
testcase_05 AC 157 ms
54,132 KB
testcase_06 WA -
testcase_07 AC 494 ms
68,204 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 498 ms
68,388 KB
testcase_11 AC 1,030 ms
108,320 KB
testcase_12 AC 148 ms
54,096 KB
testcase_13 AC 659 ms
60,488 KB
testcase_14 AC 342 ms
64,340 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 AC 353 ms
64,140 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.io.PrintWriter;
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);
					}
				}
			}
		}
		
		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);
				
				HashSet<Integer> simple_nexts = new HashSet<Integer>();
				for(final int next_id : adjs.get(uf_id)){
					final int next_uf_id = uf.find(next_id);
					simple_nexts.add(next_uf_id);
				}
				adjs.set(uf_id, simple_nexts);
			}
		}
		
		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_id : adjs.get(uf_id)){
				//System.out.println(i + " : " + (next_uf_id / W) + " " + (next_uf_id % W));
				final int next_uf_id = uf.find(next_id);
				
				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);
			}
		}
		
		PrintWriter pw = new PrintWriter(System.out);
		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){
					pw.print(" ");
				}
				pw.print(colors[uf_id / W][uf_id % W] ? 1 : 0);
			}
			pw.println();
		}
		pw.flush();
		
	}

}
0