結果
問題 | No.1078 I love Matrix Construction |
ユーザー | hedwig100 |
提出日時 | 2020-06-13 18:51:36 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,947 ms / 2,000 ms |
コード長 | 2,833 bytes |
コンパイル時間 | 288 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 165,376 KB |
最終ジャッジ日時 | 2024-06-25 18:24:34 |
合計ジャッジ時間 | 17,371 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 31 ms
11,008 KB |
testcase_01 | AC | 31 ms
11,008 KB |
testcase_02 | AC | 234 ms
33,536 KB |
testcase_03 | AC | 625 ms
69,760 KB |
testcase_04 | AC | 921 ms
92,544 KB |
testcase_05 | AC | 836 ms
78,592 KB |
testcase_06 | AC | 237 ms
32,768 KB |
testcase_07 | AC | 100 ms
19,456 KB |
testcase_08 | AC | 749 ms
78,592 KB |
testcase_09 | AC | 61 ms
14,592 KB |
testcase_10 | AC | 1,947 ms
165,376 KB |
testcase_11 | AC | 960 ms
96,768 KB |
testcase_12 | AC | 1,495 ms
138,752 KB |
testcase_13 | AC | 1,686 ms
155,136 KB |
testcase_14 | AC | 1,161 ms
110,464 KB |
testcase_15 | AC | 1,600 ms
147,072 KB |
testcase_16 | AC | 90 ms
17,536 KB |
testcase_17 | AC | 31 ms
11,008 KB |
testcase_18 | AC | 177 ms
27,520 KB |
testcase_19 | AC | 385 ms
48,640 KB |
testcase_20 | AC | 379 ms
48,000 KB |
testcase_21 | AC | 39 ms
12,288 KB |
ソースコード
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()