結果

問題 No.1078 I love Matrix Construction
ユーザー roarisroaris
提出日時 2020-12-24 21:28:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,049 ms / 2,000 ms
コード長 2,945 bytes
コンパイル時間 888 ms
コンパイル使用メモリ 81,836 KB
実行使用メモリ 226,680 KB
最終ジャッジ日時 2023-10-21 15:50:48
合計ジャッジ時間 13,237 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
53,344 KB
testcase_01 AC 34 ms
53,348 KB
testcase_02 AC 255 ms
93,576 KB
testcase_03 AC 459 ms
123,540 KB
testcase_04 AC 633 ms
146,708 KB
testcase_05 AC 530 ms
127,516 KB
testcase_06 AC 196 ms
92,868 KB
testcase_07 AC 179 ms
83,700 KB
testcase_08 AC 594 ms
141,244 KB
testcase_09 AC 116 ms
79,248 KB
testcase_10 AC 1,049 ms
226,680 KB
testcase_11 AC 647 ms
149,460 KB
testcase_12 AC 841 ms
184,204 KB
testcase_13 AC 942 ms
207,672 KB
testcase_14 AC 724 ms
161,832 KB
testcase_15 AC 1,021 ms
196,244 KB
testcase_16 AC 165 ms
81,620 KB
testcase_17 AC 33 ms
53,344 KB
testcase_18 AC 231 ms
89,132 KB
testcase_19 AC 269 ms
104,784 KB
testcase_20 AC 310 ms
105,144 KB
testcase_21 AC 72 ms
76,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(250010)
 
class Scc:
    def __init__(self, N, G):
        self.N = N
        self.G = G
        self.RG = [[] for _ in range(N)]
        
        for v in range(N):
            for nv in G[v]:
                self.RG[nv].append(v)
    
    def decomp(self):
        order = []
        visited = [False]*self.N
        
        def dfs(v):
            visited[v] = True
            
            for nv in self.G[v]:
                if not visited[nv]:
                    dfs(nv)
            
            order.append(v)
            
        for v in range(self.N):
            if not visited[v]:
                dfs(v)
        
        visited = [False]*self.N
        comp = [-1]*self.N
        label = 0
        
        def rdfs(v, label):
            comp[v] = label
            visited[v] = True
            
            for nv in self.RG[v]:
                if not visited[nv]:
                    rdfs(nv, label)
            
        for v in reversed(order):
            if not visited[v]:
                rdfs(v, label)
                label += 1
        
        return label, comp
    
    def construct(self):
        label, comp = self.decomp()
        belong = [[] for _ in range(label)]
        nG = [set() for _ in range(label)]
        
        for v in range(self.N):
            for nv in self.G[v]:
                if comp[v]!=comp[nv]:
                    nG[comp[v]].add(comp[nv])
            
            belong[comp[v]].append(v)
        
        return belong, nG
 
class Two_sat:
    def __init__(self, N):
        self.N = N
        self.G = [[] for _ in range(2*N)]
        
    def add_clause(self, i, f, j, g):
        self.G[2*i+(0 if f else 1)].append(2*j+(1 if g else 0))
        self.G[2*j+(0 if g else 1)].append(2*i+(1 if f else 0))
    
    def allocate(self):
        scc = Scc(2*self.N, self.G)
        label, comp = scc.decomp()
        res = [-1]*self.N
        
        for i in range(self.N):
            if comp[2*i]==comp[2*i+1]:
                return -1
            
            res[i] = 1 if comp[2*i]<comp[2*i+1] else 0
        
        return res

N = int(input())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
U = list(map(int, input().split()))
ts = Two_sat(N*N)

for Si, Ti, Ui in zip(S, T, U):
    if Ui==0:
        for j in range(N):
            ts.add_clause((Si-1)*N+j, True, j*N+Ti-1, True)
    elif Ui==1:
        for j in range(N):
            ts.add_clause((Si-1)*N+j, False, j*N+Ti-1, True)
    elif Ui==2:
        for j in range(N):
            ts.add_clause((Si-1)*N+j, True, j*N+Ti-1, False)
    else:
        for j in range(N):
            ts.add_clause((Si-1)*N+j, False, j*N+Ti-1, False)

res = ts.allocate()

if res==-1:
    print(-1)
    exit()

ans = [[0]*N for _ in range(N)]

for i in range(N):
    for j in range(N):
        ans[i][j] = res[i*N+j]

for ans_i in ans:
    print(*ans_i)
0