結果
| 問題 |
No.470 Inverse S+T Problem
|
| コンテスト | |
| ユーザー |
👑 SPD_9X2
|
| 提出日時 | 2025-08-02 02:47:19 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 74 ms / 2,000 ms |
| コード長 | 4,472 bytes |
| コンパイル時間 | 336 ms |
| コンパイル使用メモリ | 82,648 KB |
| 実行使用メモリ | 73,832 KB |
| 最終ジャッジ日時 | 2025-08-02 02:47:24 |
| 合計ジャッジ時間 | 3,609 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 |
ソースコード
"""
2satかな
"""
import sys
import typing
class CSR:
def __init__(
self, n: int, edges: typing.List[typing.Tuple[int, int]]) -> None:
self.start = [0] * (n + 1)
self.elist = [0] * len(edges)
for e in edges:
self.start[e[0] + 1] += 1
for i in range(1, n + 1):
self.start[i] += self.start[i - 1]
counter = self.start.copy()
for e in edges:
self.elist[counter[e[0]]] = e[1]
counter[e[0]] += 1
class SCCGraph:
'''
Reference:
R. Tarjan,
Depth-First Search and Linear Graph Algorithms
'''
def __init__(self, n: int) -> None:
self._n = n
self._edges: typing.List[typing.Tuple[int, int]] = []
def num_vertices(self) -> int:
return self._n
def add_edge(self, from_vertex: int, to_vertex: int) -> None:
self._edges.append((from_vertex, to_vertex))
def scc_ids(self) -> typing.Tuple[int, typing.List[int]]:
g = CSR(self._n, self._edges)
now_ord = 0
group_num = 0
visited = []
low = [0] * self._n
order = [-1] * self._n
ids = [0] * self._n
sys.setrecursionlimit(max(self._n + 1000, sys.getrecursionlimit()))
def dfs(v: int) -> None:
nonlocal now_ord
nonlocal group_num
nonlocal visited
nonlocal low
nonlocal order
nonlocal ids
low[v] = now_ord
order[v] = now_ord
now_ord += 1
visited.append(v)
for i in range(g.start[v], g.start[v + 1]):
to = g.elist[i]
if order[to] == -1:
dfs(to)
low[v] = min(low[v], low[to])
else:
low[v] = min(low[v], order[to])
if low[v] == order[v]:
while True:
u = visited[-1]
visited.pop()
order[u] = self._n
ids[u] = group_num
if u == v:
break
group_num += 1
for i in range(self._n):
if order[i] == -1:
dfs(i)
for i in range(self._n):
ids[i] = group_num - 1 - ids[i]
return group_num, ids
def scc(self) -> typing.List[typing.List[int]]:
ids = self.scc_ids()
group_num = ids[0]
counts = [0] * group_num
for x in ids[1]:
counts[x] += 1
groups: typing.List[typing.List[int]] = [[] for _ in range(group_num)]
for i in range(self._n):
groups[ids[1][i]].append(i)
return groups
class TwoSAT:
'''
2-SAT
Reference:
B. Aspvall, M. Plass, and R. Tarjan,
A Linear-Time Algorithm for Testing the Truth of Certain Quantified Boolean
Formulas
'''
def __init__(self, n: int = 0) -> None:
self._n = n
self._answer = [False] * n
self._scc = SCCGraph(2 * n)
def add_clause(self, i: int, f: bool, j: int, g: bool) -> None:
assert 0 <= i < self._n
assert 0 <= j < self._n
self._scc.add_edge(2 * i + (0 if f else 1), 2 * j + (1 if g else 0))
self._scc.add_edge(2 * j + (0 if g else 1), 2 * i + (1 if f else 0))
def satisfiable(self) -> bool:
scc_id = self._scc.scc_ids()[1]
for i in range(self._n):
if scc_id[2 * i] == scc_id[2 * i + 1]:
return False
self._answer[i] = scc_id[2 * i] < scc_id[2 * i + 1]
return True
def answer(self) -> typing.List[bool]:
return self._answer
N = int(input())
if N > 26*2:
print ("Impossible")
sys.exit()
ts = TwoSAT(N)
U = [ input() for i in range(N) ]
for i in range(N):
for j in range(i+1,N):
a = U[i]
b = U[j]
#0 vs 0
if a[0] == b[0] or a[1:] == b[1:]:
ts.add_clause(i,1,j,1)
#0vs1
if a[0] == b[2] or a[1:] == b[:2]:
ts.add_clause(i,1,j,0)
#1vs0
if a[2] == b[0] or a[:2] == b[1:]:
ts.add_clause(i,0,j,1)
#1vs1
if a[2] == b[2] or a[:2] == b[:2]:
ts.add_clause(i,0,j,0)
if not ts.satisfiable():
print ("Impossible")
else:
ans = ts.answer()
for i,s in enumerate(U):
if ans[i]:
print (s[:2],s[2])
else:
print (s[0],s[1:])
SPD_9X2