結果

問題 No.1078 I love Matrix Construction
ユーザー roarisroaris
提出日時 2020-12-24 21:28:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,073 ms / 2,000 ms
コード長 2,945 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 82,192 KB
実行使用メモリ 218,168 KB
最終ジャッジ日時 2024-09-21 17:05:16
合計ジャッジ時間 12,864 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,704 KB
testcase_01 AC 37 ms
53,492 KB
testcase_02 AC 277 ms
93,940 KB
testcase_03 AC 511 ms
123,368 KB
testcase_04 AC 669 ms
144,992 KB
testcase_05 AC 575 ms
128,488 KB
testcase_06 AC 225 ms
93,136 KB
testcase_07 AC 199 ms
84,420 KB
testcase_08 AC 637 ms
138,388 KB
testcase_09 AC 128 ms
80,088 KB
testcase_10 AC 1,073 ms
218,168 KB
testcase_11 AC 662 ms
147,704 KB
testcase_12 AC 902 ms
184,440 KB
testcase_13 AC 997 ms
204,472 KB
testcase_14 AC 786 ms
160,496 KB
testcase_15 AC 1,019 ms
195,276 KB
testcase_16 AC 167 ms
81,920 KB
testcase_17 AC 38 ms
53,612 KB
testcase_18 AC 247 ms
89,484 KB
testcase_19 AC 290 ms
105,536 KB
testcase_20 AC 334 ms
106,072 KB
testcase_21 AC 79 ms
77,120 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