結果
問題 | No.470 Inverse S+T Problem |
ユーザー | maspy |
提出日時 | 2023-05-28 04:30:31 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,018 ms / 2,000 ms |
コード長 | 1,729 bytes |
コンパイル時間 | 127 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 56,624 KB |
最終ジャッジ日時 | 2024-06-07 23:40:58 |
合計ジャッジ時間 | 31,148 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
外部呼び出し有り |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 866 ms
56,112 KB |
testcase_01 | AC | 880 ms
56,240 KB |
testcase_02 | AC | 896 ms
56,248 KB |
testcase_03 | AC | 874 ms
56,368 KB |
testcase_04 | AC | 872 ms
56,244 KB |
testcase_05 | AC | 871 ms
56,240 KB |
testcase_06 | AC | 875 ms
55,820 KB |
testcase_07 | AC | 878 ms
55,604 KB |
testcase_08 | AC | 868 ms
55,732 KB |
testcase_09 | AC | 872 ms
56,244 KB |
testcase_10 | AC | 880 ms
56,500 KB |
testcase_11 | AC | 876 ms
56,244 KB |
testcase_12 | AC | 1,018 ms
55,984 KB |
testcase_13 | AC | 892 ms
56,116 KB |
testcase_14 | AC | 886 ms
56,112 KB |
testcase_15 | AC | 892 ms
56,108 KB |
testcase_16 | AC | 875 ms
56,496 KB |
testcase_17 | AC | 886 ms
56,244 KB |
testcase_18 | AC | 885 ms
55,988 KB |
testcase_19 | AC | 876 ms
56,364 KB |
testcase_20 | AC | 873 ms
56,240 KB |
testcase_21 | AC | 877 ms
56,244 KB |
testcase_22 | AC | 887 ms
55,988 KB |
testcase_23 | AC | 884 ms
56,624 KB |
testcase_24 | AC | 872 ms
55,984 KB |
testcase_25 | AC | 880 ms
56,440 KB |
testcase_26 | AC | 876 ms
56,248 KB |
testcase_27 | AC | 886 ms
55,992 KB |
testcase_28 | AC | 892 ms
55,732 KB |
testcase_29 | AC | 872 ms
55,856 KB |
testcase_30 | AC | 899 ms
55,728 KB |
ソースコード
#!/usr/bin/env python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import itertools import numpy as np from scipy.sparse.csgraph import connected_components N = int(readline()) if N > 52: print('Impossible') exit() words = [readline().rstrip().decode() for _ in range(N)] AB = [] for i, j in itertools.product(range(N), repeat=2): if i == j: continue S = words[i] T = words[j] if S[0] == T[0] or S[1:] == T[1:]: AB.append((2 * i, 2 * j + 1)) if S[-1] == T[-1] or S[:2] == T[:2]: AB.append((2 * i + 1, 2 * j)) if S[0] == T[-1] or S[1:] == T[:2]: AB.append((2 * i, 2 * j)) if S[-1] == T[0] or S[:2] == T[1:]: AB.append((2 * i + 1, 2 * j + 1)) G = np.zeros((N + N, N + N), np.bool_) for a, b in AB: G[a, b] = 1 C, comp = connected_components(G, directed=True, connection='strong') graph = [[] for _ in range(C)] in_deg = [0] * C for a, b in AB: ca = comp[a] cb = comp[b] if ca == cb: continue graph[ca].append(cb) in_deg[cb] += 1 order = [] deg_0 = [i for i, x in enumerate(in_deg) if x == 0] while deg_0: v = deg_0.pop() order.append(v) for w in graph[v]: in_deg[w] -= 1 if not in_deg[w]: deg_0.append(w) rank = [0] * C for i, x in enumerate(order): rank[x] = i answers = [] for i in range(N): v = 2 * i w = 2 * i + 1 rv = rank[comp[v]] rw = rank[comp[w]] if rv == rw: print('Impossible') exit() if rv > rw: answers.append((words[i][0], words[i][1:])) else: answers.append((words[i][:2], words[i][2])) for a, b in answers: print(a,b)