結果

問題 No.307 最近色塗る問題多くない?
ユーザー 37zigen37zigen
提出日時 2020-04-06 21:13:23
言語 Java19
(openjdk 21)
結果
TLE  
実行時間 -
コード長 4,818 bytes
コンパイル時間 4,807 ms
コンパイル使用メモリ 81,968 KB
実行使用メモリ 69,628 KB
最終ジャッジ日時 2023-09-21 07:11:47
合計ジャッジ時間 11,947 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
54,448 KB
testcase_01 AC 88 ms
54,208 KB
testcase_02 AC 87 ms
54,652 KB
testcase_03 AC 89 ms
54,088 KB
testcase_04 AC 166 ms
56,776 KB
testcase_05 AC 94 ms
54,188 KB
testcase_06 AC 96 ms
52,500 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
				Queue<Integer> around=new ArrayDeque<>();
				for(int v:set[cur]) {
					v=ds.root(v);
					if(c[v]==x) {
						around.add(v);
					}
				}
				for(int v:around) {
					v=ds.root(v);
					set[cur].addAll(set[v]);
					set[cur].remove(cur);
					int pre=cur;
					ds.setUnion(v, cur);
					if(pre!=ds.root(cur)) {
						set[ds.root(cur)]=set[cur];
						cur=ds.root(cur);
					}
				}
			}
		}
		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