結果

問題 No.470 Inverse S+T Problem
ユーザー 37zigen37zigen
提出日時 2016-12-25 16:04:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 2,591 bytes
コンパイル時間 4,477 ms
コンパイル使用メモリ 82,856 KB
実行使用メモリ 59,216 KB
最終ジャッジ日時 2023-08-24 01:25:57
合計ジャッジ時間 10,290 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 153 ms
57,004 KB
testcase_01 AC 122 ms
55,688 KB
testcase_02 AC 151 ms
56,964 KB
testcase_03 AC 154 ms
56,540 KB
testcase_04 AC 152 ms
56,836 KB
testcase_05 AC 153 ms
56,928 KB
testcase_06 AC 122 ms
56,144 KB
testcase_07 AC 123 ms
56,136 KB
testcase_08 AC 122 ms
55,632 KB
testcase_09 AC 155 ms
56,820 KB
testcase_10 AC 164 ms
56,984 KB
testcase_11 AC 171 ms
58,856 KB
testcase_12 AC 152 ms
56,816 KB
testcase_13 AC 164 ms
56,772 KB
testcase_14 AC 171 ms
59,216 KB
testcase_15 AC 134 ms
55,904 KB
testcase_16 AC 163 ms
56,764 KB
testcase_17 AC 166 ms
56,776 KB
testcase_18 AC 164 ms
56,940 KB
testcase_19 AC 134 ms
55,684 KB
testcase_20 AC 153 ms
56,908 KB
testcase_21 AC 172 ms
58,844 KB
testcase_22 AC 172 ms
58,864 KB
testcase_23 AC 172 ms
58,988 KB
testcase_24 AC 137 ms
55,592 KB
testcase_25 AC 137 ms
55,636 KB
testcase_26 AC 137 ms
55,944 KB
testcase_27 AC 140 ms
55,744 KB
testcase_28 AC 122 ms
55,960 KB
testcase_29 AC 122 ms
55,932 KB
testcase_30 AC 122 ms
55,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.*;

public class Q470 {
	static ArrayList<Integer>[] g;

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		if (n > 52) {
			System.out.println("Impossible");
			return;
		}
		String[] u = new String[n];
		for (int i = 0; i < n; ++i) {
			u[i] = sc.next();
		}

		g = new ArrayList[2 * n];
		for (int i = 0; i < g.length; ++i) {
			g[i] = new ArrayList<>();
		}

		for (int i = 0; i < n; ++i) {
			for (int j = i + 1; j < n; ++j) {
				if (u[i].substring(0, 1).equals(u[j].substring(0, 1))
						|| u[i].substring(1, 3).equals(u[j].substring(1, 3))) {
					g[i].add(j + n);
					g[j].add(i + n);
				}
				if (u[i].substring(0, 1).equals(u[j].substring(2, 3))
						|| u[i].substring(1, 3).equals(u[j].substring(0, 2))) {
					g[i].add(j);
					g[n + j].add(n + i);
				}
				if (u[i].substring(0, 2).equals(u[j].substring(0, 2))
						|| u[i].substring(2, 3).equals(u[j].substring(2, 3))) {
					g[i + n].add(j);
					g[j + n].add(i);
				}
				if (u[i].substring(0, 2).equals(u[j].substring(1, 3))
						|| u[i].substring(2, 3).equals(u[j].substring(0, 1))) {
					g[i + n].add(j + n);
					g[j].add(i);
				}
			}
		}
		int V = 2 * n;
		vis = new boolean[V];
		marked = new boolean[V];
		cmp = new int[V];
		low = new int[V];
		id = new int[V];
		for (int i = 0; i < V; ++i) {
			if (!marked[i]) {
				dfs(i);
			}
		}
		for (int i = 0; i < n; ++i) {
			if (cmp[i] == cmp[i + n]) {
				System.out.println("Impossible");
				return;
			}
		}
		for (int i = 0; i < n; ++i) {
			if (cmp[i] < cmp[i + n]) {
				System.out.println(u[i].substring(0, 1) + " " + u[i].substring(1, 3));
			} else {
				System.out.println(u[i].substring(0, 2) + " " + u[i].substring(2, 3));
			}
		}
	}

	static ArrayDeque<Integer> stack = new ArrayDeque<>();
	static boolean[] vis;
	static int[] low;
	static int[] id;
	static int curId = 0;
	static boolean[] marked;
	static int[] cmp;
	static int col = 0;

	static void dfs(int cur) {
		vis[cur] = true;
		stack.addFirst(cur);
		id[cur] = curId++;
		low[cur] = id[cur];
		for (int dst : g[cur]) {
			if (marked[dst])
				continue;
			if (!vis[dst]) {
				dfs(dst);
				low[cur] = Math.min(low[cur], low[dst]);
			} else {
				low[cur] = Math.min(low[cur], id[dst]);
			}
		}
		if (id[cur] == low[cur]) {
			int v;
			do {
				v = stack.removeFirst();
				cmp[v] = col;
				marked[v] = true;
			} while (v != cur);
			++col;
		}
		return;
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0