結果
問題 | No.13 囲みたい! |
ユーザー | ゆうき |
提出日時 | 2022-10-23 04:38:29 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 119 ms / 5,000 ms |
コード長 | 6,100 bytes |
コンパイル時間 | 3,002 ms |
コンパイル使用メモリ | 83,128 KB |
実行使用メモリ | 52,952 KB |
最終ジャッジ日時 | 2024-07-02 01:17:41 |
合計ジャッジ時間 | 5,491 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 106 ms
52,504 KB |
testcase_01 | AC | 105 ms
52,292 KB |
testcase_02 | AC | 119 ms
52,496 KB |
testcase_03 | AC | 116 ms
52,512 KB |
testcase_04 | AC | 114 ms
52,648 KB |
testcase_05 | AC | 117 ms
52,688 KB |
testcase_06 | AC | 112 ms
52,196 KB |
testcase_07 | AC | 115 ms
52,232 KB |
testcase_08 | AC | 118 ms
52,708 KB |
testcase_09 | AC | 119 ms
52,952 KB |
testcase_10 | AC | 109 ms
52,544 KB |
testcase_11 | AC | 117 ms
52,480 KB |
testcase_12 | AC | 106 ms
52,440 KB |
testcase_13 | AC | 111 ms
52,448 KB |
testcase_14 | AC | 107 ms
52,712 KB |
testcase_15 | AC | 106 ms
52,200 KB |
ソースコード
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.management.ManagementFactory; import java.util.Arrays; import java.util.function.IntFunction; class Main{ boolean isDebug = ManagementFactory.getRuntimeMXBean().getInputArguments().toString() .contains("-agentlib:jdwp"); final MyReader in = new MyReader(System.in); final MyWriter out = new MyWriter(System.out); public static void main(final String[] args){ new Main().exe(); } private void exe(){ input(); preCalc(); solve(); out.flush(); } int W = in.it(); int H = in.it(); int[][] M = in.it(H,W); private void input(){} private void preCalc(){} void solve(){ UnionFind uf = new UnionFind(H *W); for (int i = 0;i < H;i++) for (int j = 0;j < W;j++) { int p = i *W +j; if (i +1 < H && M[i][j] == M[i +1][j]) { int n = p +W; if (uf.same(p,n)) { out.println(true); return; } uf.merge(p,n); } if (j +1 < W && M[i][j] == M[i][j +1]) { int n = p +1; if (uf.same(p,n)) { out.println(true); return; } uf.merge(p,n); } } out.println(false); } static class UnionFind{ final int[] par; final int[] size; public UnionFind(final int n){ par = new int[n]; Arrays.setAll(par,i -> i); size = new int[n]; Arrays.fill(size,1); } int root(final int x){ return x == par[x] ? x : (par[x] = root(par[x])); } boolean same(final int u,final int v){ return root(u) == root(v); } void merge(final int x,final int y){ int px = root(x); int py = root(y); if (px == py) return; if (size[px] < size[py]) { final int t = px; px = py; py = t; } par[py] = px; size[px] += size[py]; } int size(final int x){ return size[root(x)]; } } /* 定数 */ // final static int mod = (int) 1e9 +7; final static int mod = 998244353; final static String yes = "possible"; final static String no = "impossible"; /* 入力 */ static class MyReader{ byte[] buf = new byte[1 <<16]; int head = 0; int tail = 0; InputStream in; public MyReader(final InputStream in){ this.in = in; } byte read(){ if (head == tail) { try { tail = in.read(buf); } catch (IOException e) { e.printStackTrace(); } head = 0; } return buf[head++]; } boolean isPrintable(final byte c){ return 32 < c && c < 127; } boolean isNum(final byte c){ return 47 < c && c < 58; } byte nextPrintable(){ byte ret = read(); return isPrintable(ret) ? ret : nextPrintable(); } int it(){ return (int) lg(); } int[] it(final int N){ int[] a = new int[N]; Arrays.setAll(a,i -> it()); return a; } int[][] it(final int H,final int W){ return arr(new int[H][],i -> it(W)); } int idx(){ return it() -1; } int[] idx(final int N){ int[] a = new int[N]; Arrays.setAll(a,i -> idx()); return a; } int[][] idx(final int H,final int W){ return arr(new int[H][],i -> idx(W)); } long lg(){ byte i = nextPrintable(); boolean negative = i == 45; long n = negative ? 0 : i -'0'; while (isPrintable(i = read())) n = 10 *n +i -'0'; return negative ? -n : n; } long[] lg(final int N){ long[] a = new long[N]; Arrays.setAll(a,i -> lg()); return a; } long[][] lg(final int H,final int W){ return arr(new long[H][],i -> lg(W)); } char[] ch(){ return str().toCharArray(); } char[][] ch(final int H){ return arr(new char[H][],i -> ch()); } String line(){ StringBuilder sb = new StringBuilder(); byte c; while (isPrintable(c = read()) || c == ' ') sb.append((char) c); return sb.toString(); } String str(){ StringBuilder sb = new StringBuilder(); sb.append((char) nextPrintable()); byte c; while (isPrintable(c = read())) sb.append((char) c); return sb.toString(); } String[] str(final int N){ return arr(new String[N],i -> str()); } <T> T[] arr(final T[] arr,final IntFunction<T> f){ Arrays.setAll(arr,f); return arr; } } /* 出力 */ static class MyWriter{ OutputStream out; byte[] buf = new byte[1 <<16]; byte[] ibuf = new byte[20]; int tail = 0; public MyWriter(final OutputStream out){ this.out = out; } void flush(){ try { out.write(buf,0,tail); tail = 0; } catch (IOException e) { e.printStackTrace(); } } void sp(){ write((byte) ' '); } void ln(){ write((byte) '\n'); } void write(final byte b){ buf[tail++] = b; if (tail == buf.length) flush(); } void write(final byte[] b,final int off,final int len){ for (int i = off;i < off +len;i++) write(b[i]); } void write(long n){ if (n < 0) { n = -n; write((byte) '-'); } int i = ibuf.length; do { ibuf[--i] = (byte) (n %10 +'0'); n /= 10; } while (n > 0); write(ibuf,i,ibuf.length -i); } void println(final boolean b){ println(b ? yes : no); } void println(final long n){ write(n); ln(); } public void println(final double d){ println(String.valueOf(d)); } void println(final String s){ for (byte b:s.getBytes()) write(b); ln(); } public void println(final char[] s){ for (char b:s) write((byte) b); ln(); } void println(final int[] a){ for (int i = 0;i < a.length;i++) { if (0 < i) sp(); write(a[i]); } ln(); } void println(final long[] a){ for (int i = 0;i < a.length;i++) { if (0 < i) sp(); write(a[i]); } ln(); } } }