結果
問題 | No.2377 SUM AND XOR on Tree |
ユーザー | LyricalMaestro |
提出日時 | 2024-09-05 20:02:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,806 ms / 4,000 ms |
コード長 | 2,378 bytes |
コンパイル時間 | 772 ms |
コンパイル使用メモリ | 82,468 KB |
実行使用メモリ | 285,544 KB |
最終ジャッジ日時 | 2024-09-05 20:03:28 |
合計ジャッジ時間 | 59,685 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,316 KB |
testcase_01 | AC | 50 ms
62,744 KB |
testcase_02 | AC | 42 ms
54,576 KB |
testcase_03 | AC | 42 ms
54,600 KB |
testcase_04 | AC | 42 ms
55,732 KB |
testcase_05 | AC | 42 ms
54,444 KB |
testcase_06 | AC | 42 ms
55,804 KB |
testcase_07 | AC | 43 ms
54,664 KB |
testcase_08 | AC | 3,782 ms
282,256 KB |
testcase_09 | AC | 3,763 ms
280,344 KB |
testcase_10 | AC | 3,772 ms
279,788 KB |
testcase_11 | AC | 3,745 ms
279,284 KB |
testcase_12 | AC | 3,743 ms
281,320 KB |
testcase_13 | AC | 3,708 ms
278,940 KB |
testcase_14 | AC | 3,544 ms
274,924 KB |
testcase_15 | AC | 3,721 ms
277,216 KB |
testcase_16 | AC | 3,619 ms
274,724 KB |
testcase_17 | AC | 3,708 ms
277,296 KB |
testcase_18 | AC | 80 ms
76,856 KB |
testcase_19 | AC | 81 ms
76,736 KB |
testcase_20 | AC | 80 ms
76,724 KB |
testcase_21 | AC | 78 ms
76,884 KB |
testcase_22 | AC | 74 ms
74,944 KB |
testcase_23 | AC | 322 ms
111,484 KB |
testcase_24 | AC | 327 ms
111,496 KB |
testcase_25 | AC | 3,806 ms
276,908 KB |
testcase_26 | AC | 1,732 ms
282,868 KB |
testcase_27 | AC | 1,752 ms
284,516 KB |
testcase_28 | AC | 1,745 ms
285,544 KB |
testcase_29 | AC | 1,194 ms
267,144 KB |
testcase_30 | AC | 1,193 ms
267,660 KB |
testcase_31 | AC | 1,192 ms
267,308 KB |
testcase_32 | AC | 2,087 ms
277,608 KB |
testcase_33 | AC | 2,021 ms
278,120 KB |
testcase_34 | AC | 2,055 ms
277,612 KB |
ソースコード
## https://yukicoder.me/problems/no/2377 from collections import deque MOD = 998244353 def solve(N, next_nodes, bit_a_list): # (現役で繋がろうとしている成分の現時点の1xor, すでに完結している連結成分たちの1xor のAnd) # 0: (0, 1), 1: (1, 1) dp = [[0, 0] for _ in range(N)] parents = [-2] * N parents[0] = -1 stack = deque() stack.append((0, 0)) while len(stack) > 0: v, index = stack.pop() if index == 0: dp[v][bit_a_list[v]] = 1 else: w = next_nodes[v][index - 1] new_dp = [0] * 2 for state in range(2): state_num = dp[w][state] for base_state in range(2): base_state_num = dp[v][base_state] # つなげる new_state = state ^ base_state new_dp[new_state] += (state_num * base_state_num) % MOD new_dp[new_state] %= MOD # 独立させる if state == 1: new_state = base_state new_dp[new_state] += (state_num * base_state_num) % MOD new_dp[new_state] %= MOD dp[v] = new_dp while index < len(next_nodes[v]): w = next_nodes[v][index] if w == parents[v]: index += 1 continue parents[w] = v stack.append((v, index + 1)) stack.append((w, 0)) break # 最後の仕上げ return dp[0][1] def main(): N = int(input()) next_nodes = [[] for _ in range(N)] for _ in range(N - 1): u, v = map(int, input().split()) next_nodes[u - 1].append(v - 1) next_nodes[v - 1].append(u - 1) A = list(map(int, input().split())) # Aの各ビットごとに1の数を計算 max_a = max(A) k = 0 while (1 << k) < max_a: k += 1 max_k = k answer = 0 for k in range(max_k + 1): bit_a_list = [0] * N for i in range(N): a = A[i] bit_a_list[i] = 1 if a & (1 << k) > 0 else 0 count = solve(N, next_nodes, bit_a_list) answer += (count * (1 << k)) % MOD answer %= MOD print(answer) if __name__ == '__main__': main()