結果

問題 No.470 Inverse S+T Problem
ユーザー maspymaspy
提出日時 2023-05-28 04:30:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 1,729 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 10,852 KB
実行使用メモリ 43,008 KB
最終ジャッジ日時 2023-08-27 03:38:50
合計ジャッジ時間 12,448 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 212 ms
42,608 KB
testcase_01 AC 208 ms
42,600 KB
testcase_02 AC 205 ms
42,736 KB
testcase_03 AC 205 ms
42,728 KB
testcase_04 AC 203 ms
42,872 KB
testcase_05 AC 205 ms
42,664 KB
testcase_06 AC 206 ms
42,372 KB
testcase_07 AC 202 ms
42,540 KB
testcase_08 AC 204 ms
42,348 KB
testcase_09 AC 205 ms
42,692 KB
testcase_10 AC 202 ms
42,736 KB
testcase_11 AC 209 ms
42,824 KB
testcase_12 AC 200 ms
42,828 KB
testcase_13 AC 204 ms
42,624 KB
testcase_14 AC 208 ms
43,008 KB
testcase_15 AC 207 ms
42,816 KB
testcase_16 AC 204 ms
42,704 KB
testcase_17 AC 195 ms
42,744 KB
testcase_18 AC 194 ms
42,644 KB
testcase_19 AC 203 ms
42,984 KB
testcase_20 AC 194 ms
42,700 KB
testcase_21 AC 197 ms
42,884 KB
testcase_22 AC 209 ms
42,964 KB
testcase_23 AC 201 ms
42,948 KB
testcase_24 AC 198 ms
42,952 KB
testcase_25 AC 200 ms
42,964 KB
testcase_26 AC 204 ms
42,808 KB
testcase_27 AC 208 ms
42,912 KB
testcase_28 AC 199 ms
42,292 KB
testcase_29 AC 201 ms
42,572 KB
testcase_30 AC 200 ms
42,588 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