結果

問題 No.2377 SUM AND XOR on Tree
ユーザー poyonpoyon
提出日時 2023-05-31 01:29:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,421 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 806,896 KB
最終ジャッジ日時 2023-09-21 05:10:43
合計ジャッジ時間 23,768 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
70,928 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 70 ms
70,924 KB
testcase_04 AC 73 ms
71,012 KB
testcase_05 AC 72 ms
71,292 KB
testcase_06 AC 73 ms
71,212 KB
testcase_07 AC 72 ms
70,968 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 557 ms
100,016 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 MLE -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

# ローカルだと 3500 ms くらい (パスグラフを除けば 500 ms くらい)

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)


def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        G[u].append(v)
        G[v].append(u)
    A = list(map(int, input().split()))

    B = 30
    ans = 0
    MOD = 10**9 + 7

    def dfs(u, p):
        dp = [[0] * 2 for _ in range(B)]
        for b in range(B):
            dp[b][(A[u] >> b) & 1] = 1

        for v in G[u]:
            if v == p:
                continue

            dp_new = [[0] * 2 for _ in range(B)]
            dp_v = dfs(v, u)

            for b in range(B):
                # 切る
                dp_new[b][0] += dp[b][0] * dp_v[b][1]
                dp_new[b][1] += dp[b][1] * dp_v[b][1]

                # 切らない(繋ぐ)
                dp_new[b][0] += dp[b][0] * dp_v[b][0]
                dp_new[b][1] += dp[b][0] * dp_v[b][1]
                dp_new[b][1] += dp[b][1] * dp_v[b][0]
                dp_new[b][0] += dp[b][1] * dp_v[b][1]

                dp_new[b][0] %= MOD
                dp_new[b][1] %= MOD
            dp = dp_new

        return dp

    dp = dfs(0, -1)
    for b in range(B):
        ans += (1 << b) * dp[b][1]
        ans %= MOD
    print(ans)


if __name__ == "__main__":
    main()
0