結果
問題 | No.1078 I love Matrix Construction |
ユーザー |
![]() |
提出日時 | 2020-06-12 23:19:16 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,440 bytes |
コンパイル時間 | 203 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 133,960 KB |
最終ジャッジ日時 | 2024-06-24 06:00:18 |
合計ジャッジ時間 | 10,015 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 WA * 4 |
ソースコード
def is_bipartite(graph, s):"""頂点sを含むgraphが二部グラフかどうかを判定する"""n = len(graph)stack = [s]used[s] = 0while stack:v = stack.pop()for nxt_v in graph[v]:if used[nxt_v] == -1:used[nxt_v] = used[v] ^ 1stack.append(nxt_v)elif used[nxt_v] ^ used[v] == 0:return Falsereturn Truedef to_ind(i, j, lr):if lr == 0:ans = i * n + j + n ** 2else:ans = i * n + jreturn ansn = 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] - 1ti = t[i] - 1ul, ur = u[i] % 2, u[i] // 2graph[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 = Trueused = [-1] * (2 * n * n)for i in range(2 * n * n):if used[i] != -1:continueflag &= is_bipartite(graph, i)if not flag:print(-1)exit()ans = [[0] * n for i in range(n)]for i in range(n):for j in range(n):if used[to_ind(i, j, 0)] == 0:ans[i][j] = 1else:ans[i][j] = 0for res in ans:print(*res)