結果

問題 No.1078 I love Matrix Construction
ユーザー ttrttr
提出日時 2020-06-13 19:37:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,015 ms / 2,000 ms
コード長 2,432 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 235,196 KB
最終ジャッジ日時 2023-09-08 02:14:10
合計ジャッジ時間 13,561 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,856 KB
testcase_01 AC 96 ms
71,664 KB
testcase_02 AC 291 ms
101,456 KB
testcase_03 AC 502 ms
143,980 KB
testcase_04 AC 608 ms
165,932 KB
testcase_05 AC 548 ms
152,756 KB
testcase_06 AC 278 ms
99,792 KB
testcase_07 AC 212 ms
87,084 KB
testcase_08 AC 541 ms
151,600 KB
testcase_09 AC 182 ms
82,184 KB
testcase_10 AC 1,015 ms
235,196 KB
testcase_11 AC 610 ms
162,472 KB
testcase_12 AC 870 ms
214,184 KB
testcase_13 AC 961 ms
226,232 KB
testcase_14 AC 702 ms
177,820 KB
testcase_15 AC 920 ms
220,124 KB
testcase_16 AC 202 ms
85,664 KB
testcase_17 AC 85 ms
71,716 KB
testcase_18 AC 231 ms
93,532 KB
testcase_19 AC 324 ms
112,464 KB
testcase_20 AC 331 ms
111,808 KB
testcase_21 AC 135 ms
79,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N = int(input())
S = [int(s)-1 for s in input().split()]
T = [int(t)-1 for t in input().split()]
U = [int(u) for u in input().split()]

M = N**2*2
E = [[] for _ in range(M)]
E2 = [[] for _ in range(M)]
used = [0]*M

for i in range(N):
    for j in range(N):
        l0 = (S[i]*N+j)*2
        l1 = l0+1
        r0 = (j*N+T[i])*2
        r1 = r0+1
        if U[i] == 0:
            E[l0].append(r1)
            E[r0].append(l1)
            E2[l1].append(r0)
            E2[r1].append(l0)
        elif U[i] == 1:
            E[l1].append(r1)
            E[r0].append(l0)
            E2[l0].append(r0)
            E2[r1].append(l1)
        elif U[i] == 2:
            E2[l1].append(r1)
            E2[r0].append(l0)
            E[l0].append(r0)
            E[r1].append(l1)
        else:
            E2[l0].append(r1)
            E2[r0].append(l1)
            E[l1].append(r0)
            E[r1].append(l0)

L = []
for i in range(N):
    for j in range(N):
        for k in range(2):
            n = (i*N+j)*2+k
            if used[n] == 1:
                continue
            q = deque()
            q.append(n)
            used[n] = 1
            h = deque()
            h.append(0)
            while q:
                temp = q[-1]
                if h[-1] == len(E[temp]):
                    q.pop()
                    h.pop()
                    L.append(temp)
                else:
                    j2 = E[temp][h[-1]]
                    h[-1] += 1
                    if used[j2] == 0:
                        used[j2] = 1
                        q.append(j2)
                        h.append(0)

A = [[-1]*N for _ in range(N)]
L2 = []
L.reverse()
q = deque()
for l in L:
    if used[l] == 2:
        continue
    ele = []
    used[l] = 2
    q.append(l)
    while q:
        temp = q.pop()
        ele.append(temp)
        for e in E2[temp]:
            if used[e] == 2:
                continue
            used[e] = 2
            q.append(e)
    L2.append(ele)

L2.reverse()
used = [-1]*M
for i in range(len(L2)):
    for l in L2[i]:
        used[l] = i

for i in range(N):
    for j in range(N):
        n = (i*N+j)*2
        if used[n] > used[n+1]:
            A[i][j] = 1
        elif used[n] == used[n+1]:
            A = -1
            break
        else:
            A[i][j] = 0
    if A == -1:
        break

if A == -1:
    print(A)
else:
    for i in range(N):
        print(*A[i])
0