結果

問題 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
コンパイル時間 218 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 216,268 KB
最終ジャッジ日時 2024-09-21 17:05:46
合計ジャッジ時間 12,979 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,480 KB
testcase_01 AC 35 ms
52,992 KB
testcase_02 AC 267 ms
93,676 KB
testcase_03 AC 601 ms
125,300 KB
testcase_04 AC 652 ms
144,912 KB
testcase_05 AC 562 ms
128,384 KB
testcase_06 AC 209 ms
93,440 KB
testcase_07 AC 185 ms
83,652 KB
testcase_08 AC 588 ms
139,680 KB
testcase_09 AC 124 ms
80,124 KB
testcase_10 AC 1,074 ms
216,268 KB
testcase_11 AC 649 ms
147,856 KB
testcase_12 AC 913 ms
183,024 KB
testcase_13 AC 975 ms
205,144 KB
testcase_14 AC 763 ms
159,592 KB
testcase_15 AC 1,008 ms
193,232 KB
testcase_16 AC 162 ms
82,536 KB
testcase_17 AC 34 ms
52,608 KB
testcase_18 AC 244 ms
89,472 KB
testcase_19 AC 296 ms
104,548 KB
testcase_20 AC 332 ms
106,328 KB
testcase_21 AC 77 ms
77,568 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