結果

問題 No.2377 SUM AND XOR on Tree
ユーザー ああいいああいい
提出日時 2023-07-07 23:31:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,176 ms / 4,000 ms
コード長 1,062 bytes
コンパイル時間 436 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 297,564 KB
最終ジャッジ日時 2023-09-29 01:17:18
合計ジャッジ時間 52,091 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,036 KB
testcase_01 AC 75 ms
71,436 KB
testcase_02 AC 74 ms
71,436 KB
testcase_03 AC 74 ms
71,504 KB
testcase_04 AC 74 ms
71,036 KB
testcase_05 AC 76 ms
71,208 KB
testcase_06 AC 74 ms
71,032 KB
testcase_07 AC 76 ms
71,204 KB
testcase_08 AC 2,932 ms
291,484 KB
testcase_09 AC 3,176 ms
282,368 KB
testcase_10 AC 2,949 ms
282,488 KB
testcase_11 AC 2,607 ms
292,484 KB
testcase_12 AC 2,834 ms
290,868 KB
testcase_13 AC 2,975 ms
281,740 KB
testcase_14 AC 2,545 ms
286,840 KB
testcase_15 AC 2,919 ms
281,268 KB
testcase_16 AC 2,957 ms
285,244 KB
testcase_17 AC 2,820 ms
288,480 KB
testcase_18 AC 102 ms
76,824 KB
testcase_19 AC 102 ms
76,884 KB
testcase_20 AC 95 ms
76,468 KB
testcase_21 AC 98 ms
76,848 KB
testcase_22 AC 97 ms
77,180 KB
testcase_23 AC 3,000 ms
281,744 KB
testcase_24 AC 2,691 ms
281,252 KB
testcase_25 AC 2,968 ms
283,072 KB
testcase_26 AC 1,040 ms
287,692 KB
testcase_27 AC 1,054 ms
287,916 KB
testcase_28 AC 1,041 ms
287,700 KB
testcase_29 AC 1,104 ms
297,340 KB
testcase_30 AC 1,111 ms
297,008 KB
testcase_31 AC 1,106 ms
297,564 KB
testcase_32 AC 1,398 ms
282,588 KB
testcase_33 AC 1,424 ms
282,476 KB
testcase_34 AC 1,425 ms
282,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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()))
P = 998244353

ans = 0
for i in range(30):
    dp = [[0] * 2 for _ in range(N)]
    stack = [(0,-1),(~0,-1)]

    while stack:
        now,parent = stack.pop()
        if now < 0:
            now = ~now
            for v in G[now]:
                if v != parent:
                    stack.append((v,now))
                    stack.append((~v,now))
            continue

        u = (A[now] >> i) & 1
        dp[now][u] = 1
        a = dp[now][0]
        b = dp[now][1]
        
        for v in G[now]:
            if v == parent:continue
            c = a * dp[v][0] + a * dp[v][1] + b * dp[v][1]
            d = a * dp[v][1] + b * dp[v][1] + b * dp[v][0]
            c %= P
            d %= P
            a = c
            b = d
        dp[now][0] = a
        dp[now][1] = b
    #print(dp)
    ans += dp[0][1] * (1 << i)
    ans %= P
    #print(ans)
print(ans)
0