結果

問題 No.1078 I love Matrix Construction
ユーザー tktk_snsntktk_snsn
提出日時 2020-11-28 18:51:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 595 ms / 2,000 ms
コード長 3,770 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 82,428 KB
実行使用メモリ 135,656 KB
最終ジャッジ日時 2024-09-12 23:23:27
合計ジャッジ時間 9,824 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,680 KB
testcase_01 AC 36 ms
54,912 KB
testcase_02 AC 246 ms
85,808 KB
testcase_03 AC 371 ms
97,044 KB
testcase_04 AC 469 ms
107,972 KB
testcase_05 AC 428 ms
97,528 KB
testcase_06 AC 145 ms
86,440 KB
testcase_07 AC 180 ms
80,764 KB
testcase_08 AC 459 ms
108,316 KB
testcase_09 AC 147 ms
78,820 KB
testcase_10 AC 551 ms
135,656 KB
testcase_11 AC 473 ms
108,956 KB
testcase_12 AC 447 ms
126,172 KB
testcase_13 AC 595 ms
133,484 KB
testcase_14 AC 425 ms
114,504 KB
testcase_15 AC 569 ms
127,964 KB
testcase_16 AC 178 ms
80,348 KB
testcase_17 AC 37 ms
53,152 KB
testcase_18 AC 223 ms
83,540 KB
testcase_19 AC 174 ms
89,480 KB
testcase_20 AC 188 ms
89,324 KB
testcase_21 AC 85 ms
77,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.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 = [True] * 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 = list(map(int, input().split()))
T = list(map(int, input().split()))
U = list(map(int, input().split()))

ts = TwoSAT(n * n)
for s, t, u in zip(S, T, U):
    s -= 1
    t -= 1
    for j in range(n):
        x = s * n + j
        y = j * n + t
        if u == 0:
            ts.add_clause(x, 1, y, 1)
        elif u == 1:
            ts.add_clause(x, 0, y, 1)
        elif u == 2:
            ts.add_clause(x, 1, y, 0)
        else:
            ts.add_clause(x, 0, y, 0)

if not ts.satisfiable():
    print(-1)
    exit()

ans = [[-1] * n for _ in range(n)]
flag = ts.answer()
for i, f in enumerate(flag):
    x, y = divmod(i, n)
    if f:
        ans[x][y] = 1
    else:
        ans[x][y] = 0
for a in ans:
    print(*a)
0