結果

問題 No.1078 I love Matrix Construction
ユーザー QCFiumQCFium
提出日時 2020-05-02 18:20:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,037 ms / 2,000 ms
コード長 1,839 bytes
コンパイル時間 787 ms
コンパイル使用メモリ 87,012 KB
実行使用メモリ 186,332 KB
最終ジャッジ日時 2023-08-29 03:01:57
合計ジャッジ時間 14,490 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,376 KB
testcase_01 AC 68 ms
71,384 KB
testcase_02 AC 330 ms
94,348 KB
testcase_03 AC 511 ms
118,412 KB
testcase_04 AC 641 ms
135,696 KB
testcase_05 AC 575 ms
125,460 KB
testcase_06 AC 328 ms
94,276 KB
testcase_07 AC 249 ms
85,100 KB
testcase_08 AC 559 ms
124,632 KB
testcase_09 AC 189 ms
81,052 KB
testcase_10 AC 1,037 ms
186,332 KB
testcase_11 AC 676 ms
140,268 KB
testcase_12 AC 851 ms
167,820 KB
testcase_13 AC 953 ms
178,844 KB
testcase_14 AC 732 ms
148,776 KB
testcase_15 AC 942 ms
175,256 KB
testcase_16 AC 229 ms
82,528 KB
testcase_17 AC 69 ms
71,388 KB
testcase_18 AC 297 ms
90,860 KB
testcase_19 AC 410 ms
104,864 KB
testcase_20 AC 391 ms
104,052 KB
testcase_21 AC 150 ms
79,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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