結果

問題 No.470 Inverse S+T Problem
ユーザー 👑 timitimi
提出日時 2023-11-25 17:36:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 472 ms / 2,000 ms
コード長 2,505 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 136,304 KB
最終ジャッジ日時 2023-11-25 17:36:31
合計ジャッジ時間 4,978 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,600 KB
testcase_01 AC 39 ms
55,600 KB
testcase_02 AC 40 ms
55,600 KB
testcase_03 AC 42 ms
55,600 KB
testcase_04 AC 41 ms
55,600 KB
testcase_05 AC 40 ms
55,600 KB
testcase_06 AC 456 ms
136,304 KB
testcase_07 AC 472 ms
136,304 KB
testcase_08 AC 465 ms
136,304 KB
testcase_09 AC 40 ms
55,600 KB
testcase_10 AC 43 ms
57,660 KB
testcase_11 AC 54 ms
66,340 KB
testcase_12 AC 40 ms
55,600 KB
testcase_13 AC 43 ms
57,656 KB
testcase_14 AC 54 ms
66,340 KB
testcase_15 AC 47 ms
57,656 KB
testcase_16 AC 42 ms
55,600 KB
testcase_17 AC 44 ms
57,656 KB
testcase_18 AC 43 ms
55,600 KB
testcase_19 AC 44 ms
57,656 KB
testcase_20 AC 41 ms
55,600 KB
testcase_21 AC 54 ms
66,340 KB
testcase_22 AC 58 ms
66,340 KB
testcase_23 AC 55 ms
66,340 KB
testcase_24 AC 53 ms
66,340 KB
testcase_25 AC 54 ms
66,340 KB
testcase_26 AC 53 ms
66,340 KB
testcase_27 AC 47 ms
57,656 KB
testcase_28 AC 231 ms
86,640 KB
testcase_29 AC 271 ms
79,100 KB
testcase_30 AC 202 ms
81,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
sys.setrecursionlimit(10**7)
N=int(input())
AA=[];A=[]
for i in range(N):
  S=input()
  AA.append((S,i))
  
AA=sorted(AA)
F=[]
for a,b in AA:
  F.append(b)

for S,_ in AA:  
  a,b,c,d=S[:1],S[1:],S[:2],S[2:]
  A.append((a,b,c,d))

def scc(N, G, RG):
    order = []
    used = [0]*N
    def dfs(s):
        used[s] = 1
        for t in G[s]:
            if not used[t]:
                dfs(t)
        order.append(s)
    for i in range(N):
        if not used[i]:
            dfs(i)
    group = [-1]*N
    label = 0
    order.reverse()
    for s in order:
        if group[s] != -1:
            continue
        que = deque([s])
        group[s] = label
        while que:
            v = que.popleft()
            for w in RG[v]:
                if group[w] != -1:
                    continue
                que.append(w)
                group[w] = label
        label += 1
    return group # topological ordering

G = [[] for i in range(2*N)]
RG = [[] for i in range(2*N)]
# add (a ∨ b)
# a =  x_i if neg_i = 0
# a = ~x_i if neg_i = 1
def add_edge(i, neg_i, j, neg_j):
    if neg_i:
        i0 = i+N; i1 = i
    else:
        i0 = i; i1 = i+N
    if neg_j:
        j0 = j+N; j1 = j
    else:
        j0 = j; j1 = j+N
    # add (~a ⇒ b)
    G[i1].append(j0); RG[j0].append(i1)
    # add (~b ⇒ a)
    G[j1].append(i0); RG[i0].append(j1)

# check if the formula is satisfiable
def check(group):
    for i in range(N):
        if group[i] == group[i+N]:
            return False
    return True

# assign values to variables
def assign(group):
    res = [0]*N
    for i in range(N):
        if group[i] > group[i+N]:
            res[i] = 1
    return res
G=[[] for i in range(2*N)]; RG=[[] for i in range(2*N)] 

for i in range(N-1):
  for j in range(i+1,N):
    a,b,c,d=A[i];aa,bb,cc,dd=A[j]
    e=0
    D=[a,b,aa,bb]
    if len(D)!=len(set(D)):
      add_edge(i,0,j,0)
      e+=1
    D=[a,b,cc,dd]
    if len(D)!=len(set(D)):
      add_edge(i,0,j,1)
      e+=1
    D=[c,d,aa,bb]
    if len(D)!=len(set(D)):
      add_edge(i,1,j,0)
      e+=1
    D=[c,d,cc,dd]
    if len(D)!=len(set(D)):
      add_edge(i,1,j,1)
      e+=1
    if e==4:
      print('Impossible')
      exit()
      
B=scc(2*N, G, RG)
for i in range(N):
  if B[i]==B[i+N]:
    print('Impossible')
    exit()
    
ans=[]
for i in range(N):
  if B[i]<B[i+N]:
    ans.append((F[i],A[i][0],A[i][1]))
  else:
    ans.append((F[i],A[i][2],A[i][3]))
ans=sorted(ans)
for x,y,z in ans:
  print(y,z) 

0