結果

問題 No.1078 I love Matrix Construction
ユーザー neterukunneterukun
提出日時 2020-06-12 23:07:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,226 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 87,436 KB
実行使用メモリ 134,780 KB
最終ジャッジ日時 2023-09-06 11:13:35
合計ジャッジ時間 10,788 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 76 ms
71,440 KB
testcase_02 AC 218 ms
85,736 KB
testcase_03 AC 308 ms
99,788 KB
testcase_04 AC 358 ms
107,944 KB
testcase_05 WA -
testcase_06 AC 209 ms
86,264 KB
testcase_07 AC 178 ms
80,340 KB
testcase_08 AC 323 ms
103,056 KB
testcase_09 WA -
testcase_10 AC 539 ms
134,780 KB
testcase_11 AC 379 ms
109,936 KB
testcase_12 AC 493 ms
125,132 KB
testcase_13 AC 515 ms
131,248 KB
testcase_14 AC 408 ms
114,476 KB
testcase_15 AC 500 ms
128,388 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 198 ms
83,980 KB
testcase_19 AC 246 ms
92,052 KB
testcase_20 AC 243 ms
91,744 KB
testcase_21 AC 112 ms
78,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def is_bipartite(graph, s):
    """頂点sを含むgraphが二部グラフかどうかを判定する"""
    n = len(graph)
    stack = [s]
    used[s] = 0
    while stack:
        v = stack.pop()
        for nxt_v in graph[v]:
            if used[nxt_v] == -1:
                used[nxt_v] = used[v] ^ 1
                stack.append(nxt_v)
            elif used[nxt_v] ^ used[v] == 0:
                return False
    return True

def to_ind(i, j, lr):
    if lr == 0:
        ans = i * n + j + n ** 2
    else:
        ans = i * n + j
    return ans

n = 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] - 1
        ti = t[i] - 1
        ul, ur = u[i] % 2, u[i] // 2
        graph[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 = True
used = [-1] * (2 * n * n)
for i in range(2 * n * n):
    if used[i] != -1:
        continue
    flag &= is_bipartite(graph, i)

if not flag:
    print(-1)
    exit()
0