結果

問題 No.2505 matriX cOnstRuction
ユーザー suisensuisen
提出日時 2023-07-28 20:41:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,410 ms / 2,500 ms
コード長 4,339 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 304,900 KB
最終ジャッジ日時 2023-10-13 18:18:59
合計ジャッジ時間 33,124 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 239 ms
292,244 KB
testcase_01 AC 566 ms
294,560 KB
testcase_02 AC 456 ms
294,304 KB
testcase_03 AC 435 ms
293,796 KB
testcase_04 AC 464 ms
294,284 KB
testcase_05 AC 453 ms
294,284 KB
testcase_06 AC 549 ms
294,668 KB
testcase_07 AC 366 ms
294,228 KB
testcase_08 AC 391 ms
294,176 KB
testcase_09 AC 364 ms
293,948 KB
testcase_10 AC 388 ms
293,540 KB
testcase_11 AC 400 ms
294,288 KB
testcase_12 AC 370 ms
294,212 KB
testcase_13 AC 371 ms
294,160 KB
testcase_14 AC 369 ms
294,544 KB
testcase_15 AC 387 ms
294,312 KB
testcase_16 AC 371 ms
293,620 KB
testcase_17 AC 371 ms
294,056 KB
testcase_18 AC 380 ms
294,232 KB
testcase_19 AC 379 ms
293,972 KB
testcase_20 AC 396 ms
294,064 KB
testcase_21 AC 377 ms
293,784 KB
testcase_22 AC 381 ms
293,572 KB
testcase_23 AC 376 ms
293,492 KB
testcase_24 AC 372 ms
294,064 KB
testcase_25 AC 389 ms
294,184 KB
testcase_26 AC 359 ms
293,784 KB
testcase_27 AC 365 ms
294,156 KB
testcase_28 AC 372 ms
294,280 KB
testcase_29 AC 363 ms
293,540 KB
testcase_30 AC 329 ms
293,320 KB
testcase_31 AC 316 ms
293,340 KB
testcase_32 AC 339 ms
293,184 KB
testcase_33 AC 332 ms
293,328 KB
testcase_34 AC 354 ms
293,852 KB
testcase_35 AC 308 ms
293,736 KB
testcase_36 AC 347 ms
294,028 KB
testcase_37 AC 346 ms
294,136 KB
testcase_38 AC 327 ms
293,812 KB
testcase_39 AC 371 ms
293,628 KB
testcase_40 AC 318 ms
293,584 KB
testcase_41 AC 787 ms
304,592 KB
testcase_42 AC 332 ms
293,740 KB
testcase_43 AC 832 ms
298,632 KB
testcase_44 AC 793 ms
304,892 KB
testcase_45 AC 855 ms
298,716 KB
testcase_46 AC 797 ms
304,900 KB
testcase_47 AC 832 ms
298,712 KB
testcase_48 AC 506 ms
303,684 KB
testcase_49 AC 785 ms
304,732 KB
testcase_50 AC 779 ms
304,584 KB
testcase_51 AC 1,410 ms
299,336 KB
testcase_52 AC 670 ms
298,732 KB
testcase_53 AC 638 ms
304,632 KB
testcase_54 AC 663 ms
298,720 KB
testcase_55 AC 342 ms
293,576 KB
testcase_56 AC 336 ms
293,320 KB
testcase_57 AC 339 ms
293,500 KB
testcase_58 AC 390 ms
294,360 KB
testcase_59 AC 361 ms
293,788 KB
testcase_60 AC 382 ms
294,056 KB
testcase_61 AC 329 ms
293,388 KB
testcase_62 AC 328 ms
293,384 KB
testcase_63 AC 324 ms
293,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 兄弟節点を作成する解法

from itertools import product
import sys
from typing import List

def floor_pow2(n: int):
    """
    2 ** (floor(log2(n)))
    """
    x = 1
    while (x << 1) <= n:
        x <<= 1
    return x

# nm の最大値
MAX_NM = 50000

BIT_NUM = 30
INF_COST = 1 << 30

# -------------- Binary Trie ここから -------------- #

# 最大ノード数の見積もり:
#   - 帰着後の問題の N は高々 3(n+m)
#   - 1<=n かつ 1<=m かつ nm<=MAX_NM の下で、n+m の最大値は MAX_NM + 1
#   - 各制約に対して、(根を除いて) 高々 2*BIT_NUM 個のノードが新たに生成される
# ===> 上限を 3*(MAX_NM+1)*2*BIT_NUM+1 に設定すれば十分 (+1 は根の分)
MAX_NODE_NUM = 3 * (MAX_NM + 1) * 2 * BIT_NUM + 1

# Binary Trie の各ノードに持たせるデータ
#   - child[0][i] = i 番目のノードの左子 (-1 は左子が存在しないことを表す)
#   - child[1][i] = i 番目のノードの右子 (-1 は右子が存在しないことを表す)
#   - weight[i]   = i 番目のノードとその親を結ぶ辺の重み
child = [[-1] * MAX_NODE_NUM, [-1] * MAX_NODE_NUM]
weight = [0] * MAX_NODE_NUM

# Binary Trie のノード数
node_num = 0
def create_node() -> int:
    global node_num

    # 新しいノードの ID
    new_node = node_num

    # ★ 以前のテストケースの情報が残っている可能性があるので、初期化
    child[0][new_node] = -1 # 左子を Null 節点にする
    child[1][new_node] = -1 # 右子を Null 節点にする
    weight[new_node] = 0    # 重みを 0 にする

    node_num += 1
    return new_node

def clear_trie() -> None:
    global node_num
    node_num = 0

# -------------- Binary Trie ここまで -------------- #

def solve(n: int, m: int, R: List[int], C: List[int], A: List[List[int]]):
    for i, j in product(range(n), range(m)):
        if A[0][0] ^ A[0][j] ^ A[i][0] ^ A[i][j]:
            print(-1)
            return

    clear_trie()
    root = create_node()

    # f(X) += W * [X ^ Y > Z]
    def add_weight(Y: int, Z: int, W: int):
        YZ = Y ^ Z
        cur = root
        # Binary Trie に Y^Z を追加
        for bit in reversed(range(BIT_NUM)):
            edge_label = (YZ >> bit) & 1

            if child[edge_label][cur] == -1:
                child[edge_label][cur] = create_node()

            # 辺重みの更新: 対応する Z の bit が 0 のとき、兄弟節点に +W
            if not ((Z >> bit) & 1):
                edge_label_sibling = edge_label ^ 1
                if child[edge_label_sibling][cur] == -1:
                    child[edge_label_sibling][cur] = create_node()
                weight[child[edge_label_sibling][cur]] += W

            cur = child[edge_label][cur]

    for i in range(n):
        Y = A[0][0] ^ A[i][0]
        if R[i]:
            add_weight(Y, 0, 1)
            add_weight(Y, R[i], 1)
            add_weight(Y, 2 * floor_pow2(R[i]) - 1, INF_COST)
        else:
            add_weight(Y, 0, INF_COST)

    for j in range(m):
        Y = A[0][j]
        if C[j]:
            add_weight(Y, 0, 1)
            add_weight(Y, C[j], 1)
            add_weight(Y, 2 * floor_pow2(C[j]) - 1, INF_COST)
        else:
            add_weight(Y, 0, INF_COST)

    ans = INF_COST
    
    # 辺重みを子部分木に伝播しながらコスト計算
    # ★ Binary Trieの作り方より必ず「親節点のID<子節点のID」が成り立つので、IDを昇順に走査すれば正しく伝播できる
    for cur in range(node_num):
        # nxt: curの子節点
        for nxt in (child[0][cur], child[1][cur]):
            if nxt != -1:
                # 子節点に辺重みを伝播
                weight[nxt] += weight[cur]
            else:
                # 子がいない ==> 子部分木に非零重みの辺が存在しない ==> 子部分木内の全ての葉のコストが確定
                ans = min(ans, weight[cur])

    print(ans if ans < INF_COST else -1)

if __name__ == '__main__':
    readline = sys.stdin.readline

    T = int(readline())
    for _ in range(T):
        n, m = map(int, readline().split())
        R = list(map(int, readline().split()))
        C = list(map(int, readline().split()))
        A = [list(map(int, readline().split())) for _ in range(n)]

        solve(n, m, R, C, A)
0