結果
問題 | No.470 Inverse S+T Problem |
ユーザー | N-noa21 |
提出日時 | 2024-06-09 12:00:50 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 53 ms / 2,000 ms |
コード長 | 2,484 bytes |
コンパイル時間 | 442 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 63,744 KB |
最終ジャッジ日時 | 2024-06-09 12:00:54 |
合計ジャッジ時間 | 3,225 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
53,888 KB |
testcase_01 | AC | 44 ms
54,016 KB |
testcase_02 | AC | 44 ms
54,144 KB |
testcase_03 | AC | 44 ms
54,272 KB |
testcase_04 | AC | 44 ms
53,888 KB |
testcase_05 | AC | 43 ms
54,144 KB |
testcase_06 | AC | 43 ms
54,144 KB |
testcase_07 | AC | 42 ms
54,256 KB |
testcase_08 | AC | 42 ms
54,016 KB |
testcase_09 | AC | 45 ms
53,888 KB |
testcase_10 | AC | 53 ms
54,912 KB |
testcase_11 | AC | 52 ms
63,744 KB |
testcase_12 | AC | 44 ms
54,528 KB |
testcase_13 | AC | 45 ms
55,296 KB |
testcase_14 | AC | 52 ms
63,744 KB |
testcase_15 | AC | 46 ms
56,064 KB |
testcase_16 | AC | 44 ms
54,784 KB |
testcase_17 | AC | 45 ms
55,040 KB |
testcase_18 | AC | 46 ms
54,784 KB |
testcase_19 | AC | 46 ms
55,680 KB |
testcase_20 | AC | 43 ms
54,016 KB |
testcase_21 | AC | 52 ms
63,488 KB |
testcase_22 | AC | 52 ms
63,360 KB |
testcase_23 | AC | 53 ms
63,104 KB |
testcase_24 | AC | 51 ms
63,488 KB |
testcase_25 | AC | 52 ms
63,360 KB |
testcase_26 | AC | 53 ms
63,104 KB |
testcase_27 | AC | 52 ms
63,616 KB |
testcase_28 | AC | 44 ms
53,760 KB |
testcase_29 | AC | 44 ms
54,272 KB |
testcase_30 | AC | 45 ms
53,760 KB |
ソースコード
#https://tjkendev.github.io/procon-library/python/graph/2-sat.html from collections import deque N = int(input()) if N>52: 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")