結果

問題 No.470 Inverse S+T Problem
ユーザー N-noa21N-noa21
提出日時 2024-06-09 12:00:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,484 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 54,656 KB
最終ジャッジ日時 2024-06-09 12:00:09
合計ジャッジ時間 3,114 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
54,016 KB
testcase_01 AC 48 ms
54,016 KB
testcase_02 AC 50 ms
54,144 KB
testcase_03 AC 48 ms
54,144 KB
testcase_04 AC 47 ms
54,016 KB
testcase_05 AC 48 ms
53,888 KB
testcase_06 AC 50 ms
54,016 KB
testcase_07 AC 47 ms
54,016 KB
testcase_08 AC 48 ms
54,016 KB
testcase_09 AC 49 ms
54,272 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 50 ms
53,888 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 47 ms
54,144 KB
testcase_16 AC 49 ms
54,656 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 46 ms
54,272 KB
testcase_20 AC 49 ms
54,016 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 48 ms
53,760 KB
testcase_25 AC 47 ms
53,504 KB
testcase_26 AC 47 ms
54,016 KB
testcase_27 AC 48 ms
53,760 KB
testcase_28 AC 49 ms
54,016 KB
testcase_29 AC 48 ms
54,016 KB
testcase_30 AC 47 ms
54,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#https://tjkendev.github.io/procon-library/python/graph/2-sat.html
from collections import deque
N = int(input())

if N>26:
    print("Impossible")
    exit()

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):#i(neg_i=1ならnotがついていると判別) または j(neg_j=1ならnotがついていると判別)を作ってくれる
    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


l = [input() for i in range(N)]
#1:2で割るのをTrueとする
for i in range(N):
    pt1 = l[i]
    for j in range(i+1,N):
        pt2 = l[j]
        if pt1[0] == pt2[0] or pt1[1:] == pt2[1:]:
            add_edge(i,0,j,0)
            #print(G)
        if pt1[2] == pt2[2] or pt1[:2] == pt2[:2]:
            add_edge(i,1,j,1)
        if pt1[0] == pt2[2] or pt1[1:] == pt2[:2]:
            add_edge(i,0,j,1)
        if pt1[2] == pt2[0] or pt1[:2] == pt2[1:]:
            add_edge(i,1,j,0)
#print(G)
group = scc(2*N, G, RG)
#print(group)
if check(group):
    t = assign(group)
   # print(t)
    for i in range(N):
        if t[i] == 0:
            print(l[i][0] + " " + l[i][1:])
        else:
            print(l[i][:2] + " " + l[i][2])
else:
    print("Impossible")
0