結果

問題 No.307 最近色塗る問題多くない?
ユーザー 37zigen37zigen
提出日時 2020-04-06 21:26:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,716 ms / 4,000 ms
コード長 5,031 bytes
コンパイル時間 3,583 ms
コンパイル使用メモリ 87,212 KB
実行使用メモリ 99,184 KB
最終ジャッジ日時 2023-09-21 07:32:43
合計ジャッジ時間 17,960 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
54,272 KB
testcase_01 AC 89 ms
54,508 KB
testcase_02 AC 87 ms
54,292 KB
testcase_03 AC 88 ms
54,216 KB
testcase_04 AC 143 ms
55,984 KB
testcase_05 AC 95 ms
54,416 KB
testcase_06 AC 97 ms
54,240 KB
testcase_07 AC 213 ms
59,584 KB
testcase_08 AC 363 ms
65,116 KB
testcase_09 AC 257 ms
65,688 KB
testcase_10 AC 211 ms
59,632 KB
testcase_11 AC 271 ms
64,612 KB
testcase_12 AC 93 ms
54,416 KB
testcase_13 AC 146 ms
58,292 KB
testcase_14 AC 156 ms
56,604 KB
testcase_15 AC 160 ms
58,140 KB
testcase_16 AC 159 ms
58,260 KB
testcase_17 AC 267 ms
65,684 KB
testcase_18 AC 388 ms
69,000 KB
testcase_19 AC 411 ms
69,736 KB
testcase_20 AC 152 ms
56,640 KB
testcase_21 AC 461 ms
83,448 KB
testcase_22 AC 514 ms
87,104 KB
testcase_23 AC 519 ms
87,192 KB
testcase_24 AC 540 ms
86,364 KB
testcase_25 AC 604 ms
89,500 KB
testcase_26 AC 555 ms
88,272 KB
testcase_27 AC 774 ms
89,068 KB
testcase_28 AC 1,716 ms
92,272 KB
testcase_29 AC 140 ms
57,964 KB
testcase_30 AC 532 ms
85,532 KB
testcase_31 AC 909 ms
99,184 KB
testcase_32 AC 358 ms
78,656 KB
testcase_33 AC 270 ms
62,316 KB
testcase_34 AC 320 ms
62,420 KB
testcase_35 AC 289 ms
62,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Set;

class DJSet{
	int n;
	int[] upper;
	
	public DJSet(int n) {
		this.n=n;
		upper=new int[n];
		Arrays.fill(upper, -1);
	}
	
	int root(int x) {
		return upper[x]<0?x:(upper[x]=root(upper[x]));
	}
	
	boolean equiv(int x,int y) {
		return root(x)==root(y);
	}
	
	void setUnion(int x,int y) {
		x=root(x);y=root(y);
		if(x==y)return;
		if(upper[x]<upper[y]) {
			x^=y;y^=x;x^=y;
		}
		if(upper[x]==upper[y])--upper[y];
		upper[x]=y;
	}

}

public class Main implements Runnable{

	public static void main(String[] args) {
		new Thread(null,new Main(), "" ,Runtime.getRuntime().maxMemory()).start();
	}
	
	public void run() {
		FastScanner sc=new FastScanner();
		PrintWriter pw=new PrintWriter(System.out);
		int H=sc.nextInt();
		int W=sc.nextInt();
		int[] c=new int[H*W];
		DJSet ds=new DJSet(H*W);
		Set<Integer>[] set=new HashSet[H*W];
		for(int i=0;i<H;++i) {
			for(int j=0;j<W;++j) {
				c[i+j*H]=sc.nextInt();
				set[i+j*H]=new HashSet<>();
			}
		}
		for(int i=0;i<H;++i) {
			for(int j=0;j<W;++j) {
				int[] dh= {1,-1,0,0};
				int[] dw= {0,0,-1,1};
				int cur=i+j*H;
				for(int k=0;k<4;++k) {
					int nh=i+dh[k];
					int nw=j+dw[k];
					int nxt=nh+nw*H;
					if(nh<0||nw<0||nh>=H||nw>=W)continue;
					if(c[cur]==c[nxt])ds.setUnion(cur, nxt);
				}
			}
		}
		for(int i=0;i<H;++i) {
			for(int j=0;j<W;++j) {
				int[] dh= {1,-1,0,0};
				int[] dw= {0,0,-1,1};
				int cur=i+j*H;
				for(int k=0;k<4;++k) {
					int nh=i+dh[k];
					int nw=j+dw[k];
					int nxt=nh+nw*H;
					if(nh<0||nw<0||nh>=H||nw>=W)continue;
					if(c[cur]!=c[nxt]) {
						set[ds.root(cur)].add(ds.root(nxt));
						set[ds.root(nxt)].add(ds.root(cur));
					}
				}
			}
		}		
		
		int Q=sc.nextInt();
		for(int q=0;q<Q;++q) {
			int h=sc.nextInt()-1;
			int w=sc.nextInt()-1;
			int x=sc.nextInt();
			int cur=ds.root(h+H*w);
			if(c[cur]==x)continue;
			else {
				c[cur]=x;
				Set<Integer> around=new HashSet<>();
				for(int v:set[cur]) {
					v=ds.root(v);
					if(c[v]==x) {
						around.add(v);
					}
				}
				for(int v:around) {
					v=ds.root(v);
					if(set[v].size()>set[cur].size()) {
						set[v].addAll(set[cur]);
						set[cur]=set[v];
					}else {
						set[cur].addAll(set[v]);
					}
					int pre=cur;
					ds.setUnion(v, cur);
					if(pre!=ds.root(cur)) {
						set[ds.root(cur)]=set[cur];
						cur=ds.root(cur);
					}
				}
				around.clear();
				for(int v:set[cur]) {
					if(c[v]==c[cur])around.add(v);
				}
				for(int v:around)set[cur].remove(v);
			}
		}
		for(int i=0;i<H;++i) {
			for(int j=0;j<W;++j) {
				pw.print(c[ds.root(i+j*H)]+(j==W-1?"\n":" "));

			}
		}
		pw.close();
	}
	
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    
    public double nextDouble() { return Double.parseDouble(next());}
}
0