結果

問題 No.3724 Domination
コンテスト
ユーザー 👑 kencho
提出日時 2026-09-03 04:24:14
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 538 ms / 2,000 ms
+ 48µs
コード長 1,353 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 63 ms
コンパイル使用メモリ 15,104 KB
実行使用メモリ 33,432 KB
最終ジャッジ日時 2026-09-19 12:59:05
合計ジャッジ時間 22,308 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点 20 % AC * 8
満点 80 % AC * 52
合計 2.5 * 100% = 250 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys

it = iter(map(int, sys.stdin.buffer.read().split()))
out = []
for _ in range(next(it)):
    n = next(it)
    rows = [next(it) for _ in range(n)]
    cols = [next(it) for _ in range(n)]
    if n == 1:
        out.append("1")
        continue
    if n == 2:
        out.append("-1")
        continue
    pos = [0] * (n + 1)
    for i, x in enumerate(rows):
        pos[x] = i
    forbidden = [pos[x] for x in cols]
    chosen = [0] * n
    if all(x == cols[0] for x in cols):
        if n < 5:
            out.append("-1")
            continue
        other = [i for i in range(n) if i != forbidden[0]]
        chosen = [other[j % (n - 1)] for j in range(n)]
    else:
        unused = [True] * n
        for j in range(n - 1):
            for i in range(n):
                if unused[i] and i != forbidden[j]:
                    chosen[j] = i
                    unused[i] = False
                    break
        last = unused.index(True)
        if last != forbidden[-1]:
            chosen[-1] = last
        else:
            j = 0
            while forbidden[j] == last:
                j += 1
            chosen[-1], chosen[j] = chosen[j], last
    answer = [[rows[i]] * n for i in range(n)]
    for j in range(n):
        answer[chosen[j]][j] = cols[j]
    out.extend(" ".join(map(str, row)) for row in answer)
print("\n".join(out))
0