結果
問題 | No.1078 I love Matrix Construction |
ユーザー | Salmonize |
提出日時 | 2020-06-12 22:46:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,117 ms / 2,000 ms |
コード長 | 2,230 bytes |
コンパイル時間 | 402 ms |
コンパイル使用メモリ | 82,292 KB |
実行使用メモリ | 212,720 KB |
最終ジャッジ日時 | 2024-06-24 05:31:58 |
合計ジャッジ時間 | 13,446 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
53,212 KB |
testcase_01 | AC | 47 ms
53,140 KB |
testcase_02 | AC | 282 ms
94,020 KB |
testcase_03 | AC | 485 ms
124,108 KB |
testcase_04 | AC | 638 ms
144,540 KB |
testcase_05 | AC | 574 ms
128,084 KB |
testcase_06 | AC | 218 ms
93,448 KB |
testcase_07 | AC | 182 ms
83,276 KB |
testcase_08 | AC | 548 ms
137,560 KB |
testcase_09 | AC | 143 ms
79,648 KB |
testcase_10 | AC | 1,055 ms
212,720 KB |
testcase_11 | AC | 686 ms
147,592 KB |
testcase_12 | AC | 969 ms
183,840 KB |
testcase_13 | AC | 1,117 ms
208,004 KB |
testcase_14 | AC | 796 ms
168,248 KB |
testcase_15 | AC | 1,023 ms
193,704 KB |
testcase_16 | AC | 167 ms
81,584 KB |
testcase_17 | AC | 40 ms
53,792 KB |
testcase_18 | AC | 218 ms
88,304 KB |
testcase_19 | AC | 329 ms
106,220 KB |
testcase_20 | AC | 366 ms
106,276 KB |
testcase_21 | AC | 96 ms
77,312 KB |
ソースコード
import sys sys.setrecursionlimit(6666666) readline = sys.stdin.readline readall = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def StronglyConnectedComponents(N, G, RG): group_cnt = 0 used = [0] * N group = [0] * N order = list() def dfs(s): used[s] = 1 for t in G[s]: if not used[t]: dfs(t) order.append(s) def rdfs(s, col): group[s] = col used[s] = 1 for t in RG[s]: if not used[t]: rdfs(t, col) for v in range(N): if not used[v]: dfs(v) used = [0] * N for v in reversed(order): if not used[v]: rdfs(v, group_cnt) group_cnt += 1 return group_cnt, group class TwoSatisfilability: def __init__(self, N): self.N = N self.G = [list() for _ in range(N*2)] self.RG = [list() for _ in range(N*2)] self.res = [0] * N def solve(self): _, group = StronglyConnectedComponents(self.N * 2, self.G, self.RG) for i in range(self.N): if group[i] == group[i+self.N]: return False, list() self.res[i] = int(group[i] > group[i+self.N]) return True, self.res def _add_edge(self, a, b): self.G[a].append(b) self.RG[b].append(a) return def add(self, a, apos, b, bpos): ''' a V b ''' self._add_edge(a + self.N * apos, b + self.N * (bpos ^ 1)) self._add_edge(b + self.N * bpos, a + self.N * (apos ^ 1)) return def solve(): n = ni() s = [x-1 for x in nl()] t = [x-1 for x in nl()] u = nl() sat = TwoSatisfilability(n**2) for i in range(n): cs, ct, cu = s[i], t[i], u[i] for j in range(n): x = cs * n + j y = j * n + ct sat.add(x, (cu & 1) ^ 1, y, (cu >> 1) ^ 1) bo, ret = sat.solve() if not bo: print(-1) else: for i in range(n): print(*ret[i*n:(i+1)*n]) return solve()