結果

問題 No.1078 I love Matrix Construction
ユーザー vwxyzvwxyz
提出日時 2023-05-17 10:27:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,898 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 11,316 KB
実行使用メモリ 211,336 KB
最終ジャッジ日時 2023-08-21 16:33:08
合計ジャッジ時間 19,426 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,040 KB
testcase_01 AC 16 ms
8,080 KB
testcase_02 AC 254 ms
38,252 KB
testcase_03 AC 743 ms
86,276 KB
testcase_04 AC 1,097 ms
116,156 KB
testcase_05 AC 983 ms
97,972 KB
testcase_06 AC 252 ms
36,916 KB
testcase_07 AC 93 ms
19,540 KB
testcase_08 AC 879 ms
97,384 KB
testcase_09 AC 48 ms
13,024 KB
testcase_10 TLE -
testcase_11 AC 1,151 ms
121,492 KB
testcase_12 AC 1,800 ms
176,908 KB
testcase_13 TLE -
testcase_14 AC 1,340 ms
139,148 KB
testcase_15 TLE -
testcase_16 AC 80 ms
17,136 KB
testcase_17 AC 16 ms
8,116 KB
testcase_18 AC 179 ms
30,000 KB
testcase_19 AC 432 ms
58,116 KB
testcase_20 AC 417 ms
57,184 KB
testcase_21 AC 26 ms
10,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline

def SCC(N,edges):
    start = [0] * (N + 1)
    elist = [0] * len(edges)
    for e in edges:
        start[e[0] + 1] += 1
    for i in range(1, N + 1):
        start[i] += start[i - 1]
    counter = start[:]
    for e in edges:
        elist[counter[e[0]]] = e[1]
        counter[e[0]] += 1
    N = N
    now_ord = group_num = 0
    visited = []
    low = [0] * N
    order = [-1] * N
    ids = [0] * N
    parent = [-1] * N
    stack = []
    for i in range(N):
        if order[i] == -1:
            stack.append(i)
            stack.append(i)
            while stack:
                v = stack.pop()
                if order[v] == -1:
                    low[v] = order[v] = now_ord
                    now_ord += 1
                    visited.append(v)
                    for i in range(start[v], start[v + 1]):
                        to = elist[i]
                        if order[to] == -1:
                            stack.append(to)
                            stack.append(to)
                            parent[to] = v
                        else:
                            low[v] = min(low[v], order[to])
                else:
                    if low[v] == order[v]:
                        while True:
                            u = visited.pop()
                            order[u] = N
                            ids[u] = group_num
                            if u == v:
                                break
                        group_num += 1
                    if parent[v] != -1:
                        low[parent[v]] = min(low[parent[v]], low[v])
    for i, x in enumerate(ids):
        ids[i] = group_num - 1 - x
    groups = [[] for _ in range(group_num)]
    for i, x in enumerate(ids):
        groups[x].append(i)
    return groups

class TwoSAT:
    def __init__(self,N):
        self.N=N
        self.edges=[]

    def Add_Clause(self,i,f,j,g):
        assert 0<=i<self.N
        assert 0<=j<self.N
        self.edges.append((2*i+(0 if f else 1),2*j+(1 if g else 0)))
        self.edges.append((2*j+(0 if g else 1),2*i+(1 if f else 0)))

    def Satisfiable(self):
        scc=SCC(2*self.N,self.edges)
        idx=[None]*2*self.N
        for i,lst in enumerate(scc):
            for x in lst:
                idx[x]=i
        retu=[None]*self.N
        for i in range(self.N):
            if idx[2*i]==idx[2*i+1]:
                return None
            retu[i]=idx[2*i]<idx[2*i+1]
        return retu

N=int(readline())
S=list(map(int,readline().split()))
T=list(map(int,readline().split()))
U=list(map(int,readline().split()))
TSAT=TwoSAT(N**2)
for s,t,u in zip(S,T,U):
    s-=1;t-=1
    for j in range(N):
        TSAT.Add_Clause(s*N+j,u&1,j*N+t,u&2)
lst=TSAT.Satisfiable()
if lst:
    ans_lst=[[int(lst[i*N+j])^1 for j in range(N)]for i in range(N)]
    for ans in ans_lst:
        print(*ans)
else:
    print(-1)
0