結果

問題 No.1078 I love Matrix Construction
ユーザー hedwig100hedwig100
提出日時 2020-06-13 18:52:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 842 ms / 2,000 ms
コード長 2,833 bytes
コンパイル時間 450 ms
コンパイル使用メモリ 87,164 KB
実行使用メモリ 185,636 KB
最終ジャッジ日時 2023-09-08 01:20:17
合計ジャッジ時間 12,692 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,296 KB
testcase_01 AC 68 ms
71,072 KB
testcase_02 AC 260 ms
94,424 KB
testcase_03 AC 386 ms
117,980 KB
testcase_04 AC 493 ms
135,204 KB
testcase_05 AC 475 ms
128,460 KB
testcase_06 AC 245 ms
94,328 KB
testcase_07 AC 186 ms
84,364 KB
testcase_08 AC 444 ms
127,388 KB
testcase_09 AC 151 ms
80,812 KB
testcase_10 AC 842 ms
185,636 KB
testcase_11 AC 539 ms
134,236 KB
testcase_12 AC 698 ms
162,616 KB
testcase_13 AC 758 ms
181,460 KB
testcase_14 AC 563 ms
146,940 KB
testcase_15 AC 716 ms
171,028 KB
testcase_16 AC 178 ms
83,092 KB
testcase_17 AC 70 ms
71,236 KB
testcase_18 AC 230 ms
89,564 KB
testcase_19 AC 312 ms
106,388 KB
testcase_20 AC 313 ms
105,688 KB
testcase_21 AC 108 ms
77,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

class StronglyConnectedComponents:
    def __init__(self,N):
        self.N = N
        self.G = [[] for _ in range(self.N)]
        self.rG = [[] for _ in range(self.N)]
        self.group = [1] * self.N
        self.order = []
    
    def add_edge(self,u,v): # u -> v
        self.G[u].append(v)
        self.rG[v].append(u)
    
    def build(self):
        stack = []
        for s in range(self.N):
            if self.group[s] > 0:
                stack.append(s + 1)
                while stack:
                    v = stack.pop()
                    if v < 0:
                        self.order.append(- v - 1)
                    else:
                        if self.group[v - 1] > 0:
                            stack.append(-v)
                            self.group[v - 1] = -1
                            for e in self.G[v - 1]:
                                stack.append(e + 1)
        
        cnt = 0
        for s in self.order[::-1]:
            if self.group[s] < 0:
                stack.append(s)
                self.group[s] = cnt
                while stack:
                    v = stack.pop()
                    for e in self.rG[v]:
                        if self.group[e] < 0:
                            self.group[e] = cnt
                            stack.append(e)
                cnt += 1
    
    def __getitem__(self,i):
        if 0 <= i < self.N:
            return self.group[i]
        else:
            raise ValueError("StrongglyConnectedComponents index out of range")


def main():
    N = int(input())
    S = list(map(int,input().split()))
    T = list(map(int,input().split()))
    U = list(map(int,input().split()))
    
    M = N*N
    scc = StronglyConnectedComponents(2*M)
    for s,t,u in zip(S,T,U):
        s -= 1
        t -= 1
        if u == 0:
            for i in range(N):
                scc.add_edge(N*s + i,N*i + t + M)
                scc.add_edge(N*i + t,N*s + i + M)
        elif u == 1:
            for i in range(N):
                scc.add_edge(N*s + i + M,N*i + t + M)
                scc.add_edge(N*i + t,N*s + i)
        elif u == 2:
            for i in range(N):
                scc.add_edge(N*s + i,N*i + t)
                scc.add_edge(N*i + t + M,N*s + i + M)
        else:
            for i in range(N):
                scc.add_edge(N*s + i + M,N*i + t)
                scc.add_edge(N*i + t + M,N*s + i)
    
    scc.build()
    ans = [[0]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            a = scc[N*i + j]
            b = scc[N*i + j + M]
            if a == b:
                print(-1)
                return
            elif a < b:
                ans[i][j] = 1

    for a in ans:
        print(*a)
if __name__ == '__main__':
    main()
0