結果

問題 No.470 Inverse S+T Problem
ユーザー rlangevinrlangevin
提出日時 2024-02-04 14:13:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 4,206 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 81,720 KB
実行使用メモリ 82,176 KB
最終ジャッジ日時 2024-09-28 11:16:28
合計ジャッジ時間 3,544 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,736 KB
testcase_01 AC 38 ms
52,992 KB
testcase_02 AC 38 ms
53,120 KB
testcase_03 AC 38 ms
53,248 KB
testcase_04 AC 38 ms
53,248 KB
testcase_05 AC 37 ms
52,864 KB
testcase_06 AC 107 ms
82,024 KB
testcase_07 AC 101 ms
82,176 KB
testcase_08 AC 101 ms
82,176 KB
testcase_09 AC 37 ms
52,864 KB
testcase_10 AC 39 ms
54,272 KB
testcase_11 AC 48 ms
62,080 KB
testcase_12 AC 39 ms
53,120 KB
testcase_13 AC 40 ms
54,144 KB
testcase_14 AC 48 ms
62,848 KB
testcase_15 AC 42 ms
54,912 KB
testcase_16 AC 39 ms
53,248 KB
testcase_17 AC 39 ms
54,144 KB
testcase_18 AC 38 ms
53,632 KB
testcase_19 AC 39 ms
54,400 KB
testcase_20 AC 37 ms
53,376 KB
testcase_21 AC 47 ms
62,464 KB
testcase_22 AC 51 ms
62,848 KB
testcase_23 AC 47 ms
62,720 KB
testcase_24 AC 46 ms
62,208 KB
testcase_25 AC 45 ms
61,824 KB
testcase_26 AC 47 ms
62,336 KB
testcase_27 AC 46 ms
62,208 KB
testcase_28 AC 63 ms
72,704 KB
testcase_29 AC 58 ms
67,072 KB
testcase_30 AC 59 ms
69,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class DirectedGraph():
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]
        self.rG = [[] for i in range(N)]
        self.order = []
        self.used1 = [0] * N
        self.used2 = [0] * N
        self.group = [-1] * N
        self.label = 0
        self.seen = [0] * N
        self.Edge = set()

    def add_edge(self, u, v):
        #多重辺は排除する
        if (u, v) not in self.Edge:
            self.G[u].append(v)
            self.rG[v].append(u)
            self.Edge.add((u, v))

    def dfs(self, s):
        stack = [~s, s]
        while stack:
            u = stack.pop()
            if u >= 0:
                if self.used1[u]:
                    continue
                self.used1[u] = 1
                for v in self.G[u]:
                    if self.used1[v]:
                        continue
                    stack.append(~v)
                    stack.append(v)
            else:
                u = ~u
                if self.seen[u]:
                    continue
                self.seen[u]= 1
                self.order.append(u)

    def rdfs(self, s, num):
        stack = [s]
        while stack:
            u = stack.pop()
            if u >= 0:
                self.used2[u] = 1
                self.group[u] = num
                for v in self.rG[u]:
                    if self.used2[v]:
                        continue
                    stack.append(v)

    def scc(self):
        for i in range(self.N):
            if self.used1[i]:
                continue
            self.dfs(i)
        for s in reversed(self.order):
            if self.used2[s]:
                continue
            self.rdfs(s, self.label)
            self.label += 1
        return self.label, self.group

    def construct(self):
        nG = [set() for _ in range(self.label)]
        mem = [[] for i in range(self.label)]
        for s in range(self.N):
            now = self.group[s]
            for u in self.G[s]:
                if now == self.group[u]:
                    continue
                nG[now].add(self.group[u])
            mem[now].append(s)
        return nG, mem


class TwoSAT():
    def __init__(self, N):
        self.G = DirectedGraph(2 * N)
        
    def add(self, x1, x2, f1, f2):
        if f1 == True and f2 == True:
            # ¬x1∪¬x2
            # (x1⇒¬x2)∩(x2⇒¬x1)
            self.G.add_edge(x1, x2 + N)
            self.G.add_edge(x2, x1 + N)
            
        if f1 == True and f2 == False:
            # ¬x1∪x2
            # (x1⇒x2)∩(¬x2⇒¬x1)
            self.G.add_edge(x1, x2)
            self.G.add_edge(x2 + N, x1 + N)
        
        if f1 == False and f2 == True:
            # x1∪¬x2
            # (¬x1⇒¬x2)∩(x2⇒x1)
            self.G.add_edge(x1 + N, x2 + N)
            self.G.add_edge(x2, x1)
            
        if f1 == False and f2 == False:
            # x1∪x2
            # (¬x1⇒x2)∩(¬x2⇒x1)
            self.G.add_edge(x1 + N, x2)
            self.G.add_edge(x2 + N, x1)
            
    def check(self):
        _, group = self.G.scc()
        ans = []
        for i in range(N):
            if group[i] == group[i + N]:
                print("Impossible")
                exit()
            if group[i] > group[i + N]:
                ans.append(1)
            else:
                ans.append(0)
        return ans

N = int(input())
U = []
for i in range(N):
    U.append(input())
    
if N >= 53:
    print("Impossible")
    exit()
    
TS = TwoSAT(N)
for i in range(N):
    for j in range(i + 1, N):
        ui01, ui13, ui02, ui23 = U[i][0:1], U[i][1:3], U[i][0:2], U[i][2:3]
        uj01, uj13, uj02, uj23 = U[j][0:1], U[j][1:3], U[j][0:2], U[j][2:3]
        if ui01 == uj01 or ui13 == uj13:
            TS.add(i, j, True, True)
        if ui01 == uj23 or ui13 == uj02:
            TS.add(i, j, True, False)
        if ui23 == uj01 or ui02 == uj13:
            TS.add(i, j, False, True)
        if ui23 == uj23 or ui02 == uj02:
            TS.add(i, j, False, False)
            
ans = TS.check()
for i in range(N):
    if ans[i]:
        print(U[i][0:1], U[i][1:3])
    else:
        print(U[i][0:2], U[i][2:3])
0