結果

問題 No.470 Inverse S+T Problem
ユーザー maspymaspy
提出日時 2020-03-19 02:31:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,728 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 10,912 KB
実行使用メモリ 42,904 KB
最終ジャッジ日時 2023-08-24 01:38:34
合計ジャッジ時間 7,944 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 194 ms
42,628 KB
testcase_07 AC 193 ms
42,476 KB
testcase_08 AC 198 ms
42,528 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 AC 185 ms
42,504 KB
testcase_29 AC 188 ms
42,696 KB
testcase_30 AC 186 ms
42,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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)
0