結果
問題 | No.1078 I love Matrix Construction |
ユーザー |
![]() |
提出日時 | 2020-06-12 23:00:35 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,875 bytes |
コンパイル時間 | 352 ms |
コンパイル使用メモリ | 82,116 KB |
実行使用メモリ | 133,808 KB |
最終ジャッジ日時 | 2024-06-24 05:46:25 |
合計ジャッジ時間 | 10,295 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 WA * 4 |
ソースコード
def is_bipartite(graph, s): """頂点sを含むgraphが二部グラフかどうかを判定する""" n = len(graph) stack = [s] used[s] = 0 while stack: v = stack.pop() for nxt_v in graph[v]: if used[nxt_v] == -1: used[nxt_v] = used[v] ^ 1 stack.append(nxt_v) elif used[nxt_v] ^ used[v] == 0: return False return True def make_bipartite(graph, root): n = len(graph) stack = [root] col[root] = 0 while stack: v = stack.pop() for nxt_v in graph[v]: if col[nxt_v] != -1: continue col[nxt_v] = col[v] ^ 1 stack.append(nxt_v) def to_ind(i, j, lr): if lr == 0: ans = i * n + j + n ** 2 else: ans = i * n + j return ans n = int(input()) s = list(map(int, input().split())) t = list(map(int, input().split())) u = list(map(int, input().split())) graph = [[] for i in range(2 * n * n)] for i in range(n): for j in range(n): si = s[i] - 1 ti = t[i] - 1 ul, ur = u[i] % 2, u[i] // 2 graph[to_ind(si, j, ul)].append(to_ind(j, ti, ur)) graph[to_ind(j, ti, ur)].append(to_ind(si, j, ul)) for i in range(n ** 2): graph[i].append(i + n ** 2) graph[i + n ** 2].append(i) flag = True used = [-1] * (2 * n * n) for i in range(2 * n * n): if used[i] != -1: continue flag &= is_bipartite(graph, i) if not flag: print(-1) exit() used = [False] * (2 * n * n) col = [-1] * (2 * n * n) for i in range(2 * n * n): if col[i] != -1: continue make_bipartite(graph, i) ans = [[0] * n for i in range(n)] for i in range(n): for j in range(n): if col[to_ind(i, j, 0)] == 1: ans[i][j] = 1 else: ans[i][j] = 0 for res in ans: print(*res)