結果

問題 No.1078 I love Matrix Construction
ユーザー SalmonizeSalmonize
提出日時 2020-06-12 23:11:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 695 ms / 2,000 ms
コード長 2,713 bytes
コンパイル時間 484 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 187,680 KB
最終ジャッジ日時 2023-09-06 11:16:24
合計ジャッジ時間 9,467 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,484 KB
testcase_01 AC 65 ms
71,460 KB
testcase_02 AC 167 ms
93,248 KB
testcase_03 AC 311 ms
118,116 KB
testcase_04 AC 393 ms
139,728 KB
testcase_05 AC 355 ms
127,836 KB
testcase_06 AC 169 ms
91,980 KB
testcase_07 AC 132 ms
83,696 KB
testcase_08 AC 341 ms
127,108 KB
testcase_09 AC 134 ms
80,036 KB
testcase_10 AC 695 ms
187,680 KB
testcase_11 AC 403 ms
142,840 KB
testcase_12 AC 599 ms
173,020 KB
testcase_13 AC 627 ms
176,192 KB
testcase_14 AC 477 ms
147,780 KB
testcase_15 AC 594 ms
172,856 KB
testcase_16 AC 138 ms
82,036 KB
testcase_17 AC 68 ms
71,352 KB
testcase_18 AC 166 ms
90,116 KB
testcase_19 AC 244 ms
107,088 KB
testcase_20 AC 227 ms
105,796 KB
testcase_21 AC 110 ms
78,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def StronglyConnectedComponents(N, G, RG):
    group_cnt = 0
    used = [0] * N
    group = [0] * N
    order = list()

    for s in range(N):
        if not used[s]:
            st = [s]
            used[s] = 1
            while st:
                v = st[-1]
                fl = 0
                if used[v] <= len(G[v]):
                    if not used[G[v][used[v]-1]]:
                        st.append(G[v][used[v]-1])
                        used[G[v][used[v]-1]] = 1
                    used[v] += 1
                else:
                    order.append(v)
                    st.pop()
  
    used = [0] * N

    for s in reversed(order):
        if not used[s]:
            st = [s]
            used[s] = 1
            while st:
                v = st[-1]
                if used[v] <= len(RG[v]):
                    if not used[RG[v][used[v]-1]]:
                        st.append(RG[v][used[v]-1])
                        used[RG[v][used[v]-1]] = 1
                    used[v] += 1
                else:
                    group[v] = group_cnt
                    st.pop()
            group_cnt += 1

    return group_cnt, group


class TwoSatisfilability:
    def __init__(self, N):
        self.N = N
        self.G = [list() for _ in range(N*2)]
        self.RG = [list() for _ in range(N*2)]
        self.res = [0] * N

    def solve(self):
        _, group = StronglyConnectedComponents(self.N * 2, self.G, self.RG)
        for i in range(self.N):
            if group[i] == group[i+self.N]:
                return False, list()
            self.res[i] = int(group[i] > group[i+self.N])
        return True, self.res

    def _add_edge(self, a, b):
        self.G[a].append(b)
        self.RG[b].append(a)
        return

    def add(self, a, apos, b, bpos):
        '''
        a V b
        '''
        self._add_edge(a + self.N * apos, b + self.N * (bpos ^ 1))
        self._add_edge(b + self.N * bpos, a + self.N * (apos ^ 1))
        return


def solve():
    n = ni()
    s = [x-1 for x in nl()]
    t = [x-1 for x in nl()]
    u = nl()
    sat = TwoSatisfilability(n**2)
    for i in range(n):
        cs, ct, cu = s[i], t[i], u[i]
        for j in range(n):
            x = cs * n + j
            y = j * n + ct
            sat.add(x, (cu & 1) ^ 1, y, (cu >> 1) ^ 1)
    bo, ret = sat.solve()
    if not bo:
        print(-1)
    else:
        for i in range(n):
            print(*ret[i*n:(i+1)*n])
    return

solve()
0