結果

問題 No.1078 I love Matrix Construction
ユーザー ttrttr
提出日時 2020-06-13 15:40:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,863 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 11,412 KB
実行使用メモリ 196,424 KB
最終ジャッジ日時 2023-09-07 18:49:25
合計ジャッジ時間 8,423 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
16,200 KB
testcase_01 AC 20 ms
9,040 KB
testcase_02 AC 642 ms
59,560 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 AC 603 ms
57,680 KB
testcase_07 AC 205 ms
28,588 KB
testcase_08 TLE -
testcase_09 WA -
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

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()]

dic = {}
dic2 = {}
used = {}
for i in range(N):
    for j in range(N):
        dic[(i, j, 0)] = []
        dic[(i, j, 1)] = []
        dic2[(i, j, 0)] = []
        dic2[(i, j, 1)] = []
        used[(i, j, 0)] = 0
        used[(i, j, 1)] = 0

for i in range(N):
    for j in range(N):
        if U[i] == 0:
            dic[(S[i], j, 0)].append((j, T[i], 1))
            dic[(j, T[i], 0)].append((S[i], j, 1))
            dic2[(S[i], j, 1)].append((j, T[i], 0))
            dic2[(j, T[i], 1)].append((S[i], j, 0))
        elif U[i] == 1:
            dic[(S[i], j, 1)].append((j, T[i], 1))
            dic[(j, T[i], 0)].append((S[i], j, 0))
            dic2[(S[i], j, 0)].append((j, T[i], 0))
            dic2[(j, T[i], 1)].append((S[i], j, 1))
        elif U[i] == 2:
            dic[(S[i], j, 0)].append((j, T[i], 0))
            dic[(j, T[i], 1)].append((S[i], j, 1))
            dic2[(S[i], j, 1)].append((j, T[i], 1))
            dic2[(j, T[i], 0)].append((S[i], j, 0))
        else:
            dic[(S[i], j, 1)].append((j, T[i], 0))
            dic[(j, T[i], 1)].append((S[i], j, 0))
            dic2[(S[i], j, 0)].append((j, T[i], 1))
            dic2[(j, T[i], 0)].append((S[i], j, 1))

q = deque()
L = []
for i in range(N):
    for j in range(N):
        for k in range(2):
            ele = []
            if used[(i, j, k)] == 1:
                continue
            q.append((i, j, k))
            used[(i, j, k)] = 1
            while q:
                temp = q.pop()
                ele.append(temp)
                for e in dic[temp]:
                    if used[e] == 1:
                        continue
                    used[e] = 1
                    q.append(e)
            ele.reverse()
            L.extend(ele)

A = [[0]*N for _ in range(N)]
L.reverse()
L2 = []
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 dic2[temp]:
            if used[e] == 2:
                continue
            used[e] = 2
            q.append(e)
    L2.append(ele)

L2.reverse()
cnt = 2
for ele in L2:
    cnt += 1
    for l in ele:
        if used[(l[0], l[1], (l[2]+1)%2)] == cnt:
            A = -1
            break
        if used[(l[0], l[1], (l[2]+1)%2)] > 2:
            continue
        A[l[0]][l[1]] = l[2]
        used[l] = cnt
    if A == -1:
        break

if A == -1:
    print(A)
else:
    flag = False
    for i in range(N):
        for j in range(N):
            if A[S[i]][j]+2*A[j][T[i]] == U[i]:
                flag = True
                break
    if flag:
        print(-1)
    else:
        for i in range(N):
            print(*A[i])
0