結果
問題 | No.470 Inverse S+T Problem |
ユーザー | tanzaku |
提出日時 | 2016-12-20 00:23:45 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 87 ms / 2,000 ms |
コード長 | 8,996 bytes |
コンパイル時間 | 3,827 ms |
コンパイル使用メモリ | 91,688 KB |
実行使用メモリ | 53,056 KB |
最終ジャッジ日時 | 2024-06-01 22:27:06 |
合計ジャッジ時間 | 7,048 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 72 ms
51,168 KB |
testcase_01 | AC | 48 ms
50,048 KB |
testcase_02 | AC | 75 ms
51,260 KB |
testcase_03 | AC | 73 ms
51,356 KB |
testcase_04 | AC | 71 ms
51,160 KB |
testcase_05 | AC | 80 ms
51,264 KB |
testcase_06 | AC | 48 ms
50,216 KB |
testcase_07 | AC | 48 ms
50,056 KB |
testcase_08 | AC | 48 ms
50,244 KB |
testcase_09 | AC | 77 ms
51,184 KB |
testcase_10 | AC | 75 ms
51,352 KB |
testcase_11 | AC | 87 ms
51,148 KB |
testcase_12 | AC | 73 ms
51,352 KB |
testcase_13 | AC | 80 ms
51,544 KB |
testcase_14 | AC | 79 ms
51,356 KB |
testcase_15 | AC | 52 ms
50,316 KB |
testcase_16 | AC | 73 ms
51,452 KB |
testcase_17 | AC | 75 ms
51,180 KB |
testcase_18 | AC | 77 ms
51,180 KB |
testcase_19 | AC | 49 ms
50,032 KB |
testcase_20 | AC | 77 ms
53,056 KB |
testcase_21 | AC | 78 ms
51,316 KB |
testcase_22 | AC | 82 ms
51,196 KB |
testcase_23 | AC | 86 ms
51,516 KB |
testcase_24 | AC | 51 ms
50,044 KB |
testcase_25 | AC | 50 ms
50,260 KB |
testcase_26 | AC | 51 ms
50,008 KB |
testcase_27 | AC | 52 ms
50,328 KB |
testcase_28 | AC | 47 ms
50,152 KB |
testcase_29 | AC | 48 ms
52,240 KB |
testcase_30 | AC | 48 ms
50,328 KB |
ソースコード
import java.io.*; import java.math.*; import java.util.*; import static java.util.Arrays.*; public class _470 { private static final int mod = (int)1e9+7; final Random random = new Random(0); final IOFast io = new IOFast(); /// MAIN CODE public void run() throws IOException { // int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim()); int TEST_CASE = 1; while(TEST_CASE-- != 0) { int n = io.nextInt(); if (n > 52) { io.out.println("Impossible"); return; } Sat2 sat = new Sat2(2*n); String[][][] cs = new String[n][2][2]; for (int i = 0; i < n; i++) { String s = io.nextString(); cs[i][0][0] = s.substring(0, 1); cs[i][0][1] = s.substring(1, 3); cs[i][1][0] = s.substring(2, 3); cs[i][1][1] = s.substring(0, 2); sat.Or(sat.Not(2*i+0), sat.Not(2*i+1)); sat.Or(2*i+0, 2*i+1); for (int j = 0; j < i; j++) { for (int k = 0; k < 2; k++) { for (int l = 0; l < 2; l++) { if (cs[i][k][0].equals(cs[j][l][0]) || cs[i][k][1].equals(cs[j][l][1])) { sat.Or(sat.Not(2*i+k), sat.Not(2*j+l)); // dump(i, j, k, l); } } } } } if (!sat.satisfy()) { io.out.println("Impossible"); return; } for (int i = 0; i < n; i++) { swap(cs[i][1], 0, 1); } for (int i = 0; i < 2*n; i++) { if (sat.ans[i]) { io.out.println(cs[i/2][i%2][0] + " " + cs[i/2][i%2][1]); } } } } int toNum(char c) { if (Character.isLowerCase(c)) return c - 'a'; return c - 'A' + 26; } static public class Sat2 { final int n; final boolean[] ans; final StronglyConnectedComponent scc; void clear() { scc.clear(); } public Sat2(int n) { this.n = n; ans = new boolean[n]; scc = new StronglyConnectedComponent(n * 2); } private int Not(int a) { if(a < 0 || a >= 2 * n) throw new RuntimeException(); return a >= n ? a - n : a + n; } public void Or(int a, int b) { if(a < 0 || a >= 2 * n || b < 0 || b >= 2 * n) throw new RuntimeException(); scc.addEdge(Not(a), b); scc.addEdge(Not(b), a); } public boolean satisfy() { scc.scc(); for(int i = 0; i < n; i++) { // 同じ強連結成分に含まれたら充足不可能 if(scc.cmp[i] == scc.cmp[i + n]) { return false; } } for(int i = 0; i < n; i++) { ans[i] = scc.cmp[i] > scc.cmp[i + n]; } return true; } static public class StronglyConnectedComponent { int V; List<List<Integer>> G; List<List<Integer>> rG; List<Integer> vs; boolean[] used; public int[] cmp; void clear() { for (int i = 0; i < V; i++) { G.get(i).clear(); rG.get(i).clear(); } } public StronglyConnectedComponent(int n) { V = n; G = new ArrayList<List<Integer>>(n); rG = new ArrayList<List<Integer>>(n); vs = new ArrayList<Integer>(); used = new boolean[n]; cmp = new int[n]; for (int i = 0; i < n; i++) { G.add(new ArrayList<Integer>()); rG.add(new ArrayList<Integer>()); } } public void addEdge(int from, int to) { G.get(from).add(to); rG.get(to).add(from); } private void dfs(int v) { used[v] = true; for (int i = 0; i < G.get(v).size(); i++) if (!used[G.get(v).get(i)]) dfs(G.get(v).get(i)); vs.add(v); } private void rdfs(int v, int k) { used[v] = true; cmp[v] = k; for (int i = 0; i < rG.get(v).size(); i++) if (!used[rG.get(v).get(i)]) rdfs(rG.get(v).get(i), k); } public int scc() { for (int i = 0; i < V; i++) used[i] = false; vs.clear(); for (int i = 0; i < V; i++) if (!used[i]) dfs(i); for (int i = 0; i < V; i++) used[i] = false; int k = 0; for (int i = vs.size() - 1; i >= 0; i--) if (!used[vs.get(i)]) rdfs(vs.get(i), k++); return k; } } } /// TEMPLATE static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); } static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); } static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; } static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; } void printArrayLn(int[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); } void printArrayLn(long[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); } static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); } void main() throws IOException { // IOFast.setFileIO("rle-size.in", "rle-size.out"); try { run(); } catch (EndOfFileRuntimeException e) { } io.out.flush(); } public static void main(String[] args) throws IOException { new _470().main(); } static class EndOfFileRuntimeException extends RuntimeException { private static final long serialVersionUID = -8565341110209207657L; } static public class IOFast { private InputStream in = System.in; private PrintWriter out = new PrintWriter(System.out); private static int pos, readLen; private static final byte[] buffer = new byte[1024 * 8]; private static char[] str = new char[500*8*2]; private static boolean[] isDigit = new boolean[256]; private static boolean[] isSpace = new boolean[256]; private static boolean[] isLineSep = new boolean[256]; static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; } public byte read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; } public int nextInt() throws IOException { byte first = (byte)nextChar(); boolean minus = false; int res = 0; if (first == '-') { minus = true; } else { res = first - '0'; } while (true) { byte b = read(); if (isSpace[b]) break; res = res * 10 + b - '0'; } return minus ? -res : res; } public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; } public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } } int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; } int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; } public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); } public String nextString() throws IOException { return new String(next()); } public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); } // public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; } // public double nextDouble() throws IOException { return Double.parseDouble(nextString()); } public double nextDouble() throws IOException { double res = nextChar() - '0'; while (true) { byte c = read(); if (isSpace[c]) return res; if (c == '.') break; res = res * 10 + c - '0'; } double d = 1; while (true) { byte c = read(); // dump(c); if (!isDigit[c]) break; res = res * 10 + c - '0'; d *= 10; } // dump(1); return res / d; } public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; } public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; } public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; } } }