結果
問題 | No.1078 I love Matrix Construction |
ユーザー | QCFium |
提出日時 | 2020-05-02 18:20:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,012 ms / 2,000 ms |
コード長 | 1,839 bytes |
コンパイル時間 | 489 ms |
コンパイル使用メモリ | 82,140 KB |
実行使用メモリ | 185,784 KB |
最終ジャッジ日時 | 2024-06-09 20:13:13 |
合計ジャッジ時間 | 13,753 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,084 KB |
testcase_01 | AC | 39 ms
54,044 KB |
testcase_02 | AC | 313 ms
94,276 KB |
testcase_03 | AC | 493 ms
117,652 KB |
testcase_04 | AC | 629 ms
133,072 KB |
testcase_05 | AC | 560 ms
123,140 KB |
testcase_06 | AC | 299 ms
93,756 KB |
testcase_07 | AC | 222 ms
83,292 KB |
testcase_08 | AC | 547 ms
123,712 KB |
testcase_09 | AC | 166 ms
79,964 KB |
testcase_10 | AC | 1,012 ms
185,784 KB |
testcase_11 | AC | 666 ms
136,536 KB |
testcase_12 | AC | 883 ms
167,376 KB |
testcase_13 | AC | 985 ms
179,964 KB |
testcase_14 | AC | 714 ms
145,984 KB |
testcase_15 | AC | 947 ms
173,136 KB |
testcase_16 | AC | 211 ms
81,904 KB |
testcase_17 | AC | 38 ms
53,612 KB |
testcase_18 | AC | 271 ms
88,868 KB |
testcase_19 | AC | 390 ms
102,760 KB |
testcase_20 | AC | 377 ms
102,536 KB |
testcase_21 | AC | 122 ms
78,600 KB |
ソースコード
import sys sys.setrecursionlimit(500 * 500 * 2 + 1) N = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) for i in range(N) : a[i] -= 1 b[i] -= 1 def INDEX(i, j, k) : return (i * N + j) * 2 + k hen = [[] for i in range(N * N * 2)] rev = [[] for i in range(N * N * 2)] for i in range(N) : r0 = c[i] % 2 r1 = c[i] // 2 for j in range(N) : hen[INDEX(a[i], j, r0)].append(INDEX(j, b[i], not r1)); rev[INDEX(j, b[i], not r1)].append(INDEX(a[i], j, r0)); hen[INDEX(j, b[i], r1)].append(INDEX(a[i], j, not r0)); rev[INDEX(a[i], j, not r0)].append(INDEX(j, b[i], r1)); visited = [False for i in range(N * N * 2)] ord = [] def dfs0(i) : visited[i] = True stack = [i] head = [0] while len(stack) : i = stack[-1] if head[-1] == len(hen[i]) : stack.pop(-1) head.pop(-1) ord.append(i) else : j = hen[i][head[-1]] head[-1] += 1 if not visited[j] : visited[j] = True stack.append(j) head.append(0) group = [-1 for i in range(N * N * 2)] group_cnt = 0 def dfs1(i) : group[i] = group_cnt stack = [i] head = [0] while len(stack) : i = stack[-1] if head[-1] == len(rev[i]) : stack.pop(-1) head.pop(-1) else : j = rev[i][head[-1]] head[-1] += 1 if group[j] == -1 : stack.append(j) head.append(0) group[j] = group_cnt for i in range(N * N * 2) : if not visited[i] : dfs0(i) ord = ord[::-1] for i in ord : if group[i] == -1 : dfs1(i) group_cnt += 1 possible = True res = [[0 for j in range(N)] for i in range(N)] for i in range(N) : for j in range(N) : if group[INDEX(i, j, 0)] == group[INDEX(i, j, 1)] : possible = False res[i][j] = int(group[INDEX(i, j, 0)] < group[INDEX(i, j, 1)]) if not possible : print("-1") else : for i in range(N) : print(' '.join(map(str, res[i])))