結果

問題 No.470 Inverse S+T Problem
ユーザー tktk_snsntktk_snsn
提出日時 2020-11-28 20:01:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 3,818 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 77,952 KB
最終ジャッジ日時 2024-06-01 23:06:00
合計ジャッジ時間 3,043 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,120 KB
testcase_01 AC 40 ms
52,992 KB
testcase_02 AC 40 ms
52,992 KB
testcase_03 AC 41 ms
52,748 KB
testcase_04 AC 40 ms
52,736 KB
testcase_05 AC 40 ms
52,992 KB
testcase_06 AC 68 ms
77,568 KB
testcase_07 AC 65 ms
77,696 KB
testcase_08 AC 66 ms
77,952 KB
testcase_09 AC 40 ms
52,736 KB
testcase_10 AC 42 ms
53,420 KB
testcase_11 AC 51 ms
61,952 KB
testcase_12 AC 39 ms
52,864 KB
testcase_13 AC 41 ms
53,504 KB
testcase_14 AC 49 ms
62,336 KB
testcase_15 AC 47 ms
60,160 KB
testcase_16 AC 39 ms
53,076 KB
testcase_17 AC 40 ms
53,376 KB
testcase_18 AC 41 ms
53,760 KB
testcase_19 AC 47 ms
60,416 KB
testcase_20 AC 40 ms
53,120 KB
testcase_21 AC 49 ms
62,464 KB
testcase_22 AC 49 ms
62,336 KB
testcase_23 AC 50 ms
62,048 KB
testcase_24 AC 48 ms
60,864 KB
testcase_25 AC 50 ms
61,568 KB
testcase_26 AC 50 ms
61,696 KB
testcase_27 AC 49 ms
61,568 KB
testcase_28 AC 48 ms
63,616 KB
testcase_29 AC 45 ms
59,520 KB
testcase_30 AC 44 ms
61,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


class SCC_graph(object):
    def __init__(self, n):
        """n:ノード数"""
        self.n = n
        self.edges = []

    def add_edge(self, frm, to):
        """frm -> toへ有効辺を張る"""
        self.edges.append((frm, to))

    def __csr(self):
        self.start = [0] * (self.n + 1)
        self.elist = [0] * len(self.edges)
        for frm, to in self.edges:
            self.start[frm + 1] += 1
        for i in range(1, self.n + 1):
            self.start[i] += self.start[i - 1]
        cnt = self.start[:]
        for frm, to in self.edges:
            self.elist[cnt[frm]] = to
            cnt[frm] += 1

    def __dfs(self, v):
        self.low[v] = self.now_ord
        self.order[v] = self.now_ord
        self.now_ord += 1
        self.visited.append(v)
        for i in range(self.start[v], self.start[v + 1]):
            to = self.elist[i]
            if self.order[to] == -1:
                self.__dfs(to)
                self.low[v] = min(self.low[v], self.low[to])
            else:
                self.low[v] = min(self.low[v], self.order[to])
        if self.low[v] == self.order[v]:
            while self.visited:
                u = self.visited.pop()
                self.order[u] = self.n
                self.ids[u] = self.group_num
                if u == v:
                    break
            self.group_num += 1

    def _make_scc_ids(self):
        self.__csr()
        self.now_ord = 0
        self.group_num = 0
        self.visited = []
        self.low = [0] * self.n
        self.ids = [0] * self.n
        self.order = [-1] * self.n
        for i in range(self.n):
            if self.order[i] == -1:
                self.__dfs(i)
        for i in range(self.n):
            self.ids[i] = self.group_num - 1 - self.ids[i]

    def scc(self):
        self._make_scc_ids()
        groups = [[] for _ in range(self.group_num)]
        for i in range(self.n):
            groups[self.ids[i]].append(i)
        return groups


class TwoSAT(SCC_graph):
    def __init__(self, n):
        """ n: ノード数"""
        self._n = n
        super().__init__(2 * n)

    def add_clause(self, i, f, j, g):
        """ (xi == f)∨(xj == g)というクローズを追加 """
        x = 2 * i + (0 if f else 1)
        y = 2 * j + (1 if g else 0)
        self.add_edge(x, y)
        x = 2 * j + (0 if g else 1)
        y = 2 * i + (1 if f else 0)
        self.add_edge(x, y)

    def satisfiable(self):
        """ 条件を満たす割り当てが存在するか判定する """
        self._make_scc_ids()
        self._answer = [False] * self._n
        for i in range(self._n):
            if self.ids[2 * i] == self.ids[2 * i + 1]:
                return False
            self._answer[i] = (self.ids[2 * i] < self.ids[2 * i + 1])
        return True

    def answer(self):
        """ 最後に読んだsatisfiableのクローズを満たす割り当てを返す """
        return self._answer


n = int(input())
S = [input().rstrip() for _ in range(n)]
if n > 100:
    print("Impossible")
    exit()

ts = TwoSAT(n)
for i, s in enumerate(S):
    a1, a2 = s[0], s[1:]
    b1, b2 = s[:2], s[2]
    for j, t in enumerate(S):
        if i < j:
            c1, c2 = t[0], t[1:]
            d1, d2 = t[:2], t[2]
            if a1 == c1 or a2 == c2:
                ts.add_clause(i, 1, j, 1)
            if a1 == d2 or a2 == d1:
                ts.add_clause(i, 1, j, 0)
            if b1 == c2 or b2 == c1:
                ts.add_clause(i, 0, j, 1)
            if b1 == d1 or b2 == d2:
                ts.add_clause(i, 0, j, 0)

if not ts.satisfiable():
    print("Impossible")
    exit()

for s, f in zip(S, ts.answer()):
    if f:
        print(s[:2], s[2])
    else:
        print(s[0], s[1:])
0