結果

問題 No.1078 I love Matrix Construction
ユーザー roarisroaris
提出日時 2020-12-24 21:35:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,074 ms / 2,000 ms
コード長 2,902 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 81,592 KB
実行使用メモリ 222,004 KB
最終ジャッジ日時 2023-10-21 15:51:25
合計ジャッジ時間 12,284 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,472 KB
testcase_01 AC 37 ms
53,476 KB
testcase_02 AC 258 ms
94,056 KB
testcase_03 AC 502 ms
125,272 KB
testcase_04 AC 653 ms
146,252 KB
testcase_05 AC 563 ms
128,248 KB
testcase_06 AC 203 ms
92,904 KB
testcase_07 AC 178 ms
83,128 KB
testcase_08 AC 617 ms
140,636 KB
testcase_09 AC 122 ms
79,628 KB
testcase_10 AC 1,074 ms
222,004 KB
testcase_11 AC 695 ms
148,528 KB
testcase_12 AC 895 ms
185,124 KB
testcase_13 AC 995 ms
207,492 KB
testcase_14 AC 760 ms
161,036 KB
testcase_15 AC 1,006 ms
197,692 KB
testcase_16 AC 173 ms
81,708 KB
testcase_17 AC 36 ms
53,472 KB
testcase_18 AC 243 ms
89,352 KB
testcase_19 AC 273 ms
104,716 KB
testcase_20 AC 311 ms
106,204 KB
testcase_21 AC 76 ms
76,848 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): #0:負リテラル 1:正リテラル
        self.G[2*i+1^f].append(2*j+g)
        self.G[2*j+1^g].append(2*i+f)
    
    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, 1, j*N+Ti-1, 1)
    elif Ui==1:
        for j in range(N):
            ts.add_clause((Si-1)*N+j, 0, j*N+Ti-1, 1)
    elif Ui==2:
        for j in range(N):
            ts.add_clause((Si-1)*N+j, 1, j*N+Ti-1, 0)
    else:
        for j in range(N):
            ts.add_clause((Si-1)*N+j, 0, j*N+Ti-1, 0)

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