結果

問題 No.2377 SUM AND XOR on Tree
ユーザー minimumminimum
提出日時 2023-07-07 22:31:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,987 ms / 4,000 ms
コード長 1,176 bytes
コンパイル時間 652 ms
コンパイル使用メモリ 87,256 KB
実行使用メモリ 370,556 KB
最終ジャッジ日時 2023-09-29 00:01:56
合計ジャッジ時間 42,334 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,232 KB
testcase_01 AC 72 ms
71,420 KB
testcase_02 AC 73 ms
71,332 KB
testcase_03 AC 72 ms
71,240 KB
testcase_04 AC 71 ms
71,240 KB
testcase_05 AC 72 ms
71,064 KB
testcase_06 AC 71 ms
71,400 KB
testcase_07 AC 71 ms
71,304 KB
testcase_08 AC 3,562 ms
360,796 KB
testcase_09 AC 3,833 ms
361,792 KB
testcase_10 AC 3,303 ms
362,040 KB
testcase_11 AC 3,320 ms
361,736 KB
testcase_12 AC 3,284 ms
361,684 KB
testcase_13 AC 3,192 ms
361,464 KB
testcase_14 AC 3,072 ms
349,704 KB
testcase_15 AC 3,136 ms
359,500 KB
testcase_16 AC 3,141 ms
353,444 KB
testcase_17 AC 3,162 ms
356,928 KB
testcase_18 AC 91 ms
76,392 KB
testcase_19 AC 89 ms
76,348 KB
testcase_20 AC 87 ms
76,456 KB
testcase_21 AC 90 ms
76,468 KB
testcase_22 AC 98 ms
76,416 KB
testcase_23 AC 3,835 ms
362,372 KB
testcase_24 AC 3,987 ms
361,836 KB
testcase_25 AC 3,734 ms
361,656 KB
testcase_26 AC 1,509 ms
369,948 KB
testcase_27 AC 1,528 ms
369,732 KB
testcase_28 AC 1,537 ms
369,820 KB
testcase_29 AC 1,429 ms
370,192 KB
testcase_30 AC 1,414 ms
370,556 KB
testcase_31 AC 1,419 ms
370,096 KB
testcase_32 AC 1,656 ms
361,760 KB
testcase_33 AC 1,654 ms
362,324 KB
testcase_34 AC 1,668 ms
362,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

st = []
st.append((0, 0))
dp = [[[0, 0] for i in range(N)] for b in range(31)]
par = [-1] * N

while st:
    now, t = st.pop()
    if t == 0:
        st.append((now, 1))
        for nxt in edges[now]:
            if par[now] != nxt:
                st.append((nxt, 0))
                par[nxt] = now
    else:
        # print(now)
        for b in range(31):
            if (A[now] >> b) & 1:
                dp[b][now][1] = 1
            else:
                dp[b][now][0] = 1
            for nxt in edges[now]:
                if nxt != par[now]:
                    tmp0 = dp[b][nxt][0] * dp[b][now][0] + dp[b][nxt][1] * (dp[b][now][0] + dp[b][now][1])
                    tmp1 = dp[b][nxt][0] * dp[b][now][1] + dp[b][nxt][1] * (dp[b][now][0] + dp[b][now][1])
                    dp[b][now][0] = tmp0 % MOD
                    dp[b][now][1] = tmp1 % MOD


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