結果

問題 No.1078 I love Matrix Construction
ユーザー mkawa2mkawa2
提出日時 2022-07-13 14:39:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,361 ms / 2,000 ms
コード長 4,515 bytes
コンパイル時間 531 ms
コンパイル使用メモリ 87,272 KB
実行使用メモリ 458,416 KB
最終ジャッジ日時 2023-09-07 00:01:45
合計ジャッジ時間 16,499 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 163 ms
80,692 KB
testcase_01 AC 166 ms
80,676 KB
testcase_02 AC 403 ms
96,316 KB
testcase_03 AC 627 ms
155,084 KB
testcase_04 AC 763 ms
184,228 KB
testcase_05 AC 696 ms
113,532 KB
testcase_06 AC 286 ms
94,124 KB
testcase_07 AC 327 ms
91,432 KB
testcase_08 AC 885 ms
243,600 KB
testcase_09 AC 312 ms
88,964 KB
testcase_10 AC 1,361 ms
458,416 KB
testcase_11 AC 901 ms
201,920 KB
testcase_12 AC 745 ms
197,716 KB
testcase_13 AC 758 ms
138,424 KB
testcase_14 AC 794 ms
128,176 KB
testcase_15 AC 892 ms
263,016 KB
testcase_16 AC 321 ms
89,612 KB
testcase_17 AC 163 ms
80,440 KB
testcase_18 AC 398 ms
96,636 KB
testcase_19 AC 310 ms
100,140 KB
testcase_20 AC 344 ms
109,656 KB
testcase_21 AC 212 ms
83,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

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:
    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

# https://atcoder.github.io/ac-library/production/document_ja/twosat.html
class TwoSAT:
    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 = II()
ss = LI1()
tt = LI1()
uu = LI()

ts = TwoSAT(n**2)
for s, t, u in zip(ss, tt, uu):
    e, d = divmod(u, 2)
    for k in range(n):
        ts.add_clause(s*n+k, d ^ 1, k*n+t, e ^ 1)

if ts.satisfiable():
    aa = [[0]*n for _ in range(n)]
    cur = []
    for a in ts.answer():
        cur.append(a*1)
        if len(cur) == n:
            print(*cur)
            cur = []

else:
    print(-1)
0