結果

問題 No.1078 I love Matrix Construction
ユーザー hedwig100hedwig100
提出日時 2020-06-13 18:51:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,594 ms / 2,000 ms
コード長 2,833 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 162,716 KB
最終ジャッジ日時 2023-09-08 01:19:56
合計ジャッジ時間 16,609 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,460 KB
testcase_01 AC 17 ms
8,512 KB
testcase_02 AC 193 ms
30,856 KB
testcase_03 AC 527 ms
67,124 KB
testcase_04 AC 774 ms
90,004 KB
testcase_05 AC 697 ms
75,932 KB
testcase_06 AC 184 ms
29,980 KB
testcase_07 AC 67 ms
16,740 KB
testcase_08 AC 620 ms
75,976 KB
testcase_09 AC 38 ms
12,056 KB
testcase_10 AC 1,594 ms
162,716 KB
testcase_11 AC 873 ms
94,180 KB
testcase_12 AC 1,352 ms
135,968 KB
testcase_13 AC 1,472 ms
152,560 KB
testcase_14 AC 1,019 ms
107,700 KB
testcase_15 AC 1,387 ms
144,468 KB
testcase_16 AC 63 ms
15,192 KB
testcase_17 AC 17 ms
8,396 KB
testcase_18 AC 150 ms
24,820 KB
testcase_19 AC 337 ms
46,204 KB
testcase_20 AC 339 ms
45,360 KB
testcase_21 AC 23 ms
9,668 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