結果

問題 No.2377 SUM AND XOR on Tree
ユーザー KowerKoint2010KowerKoint2010
提出日時 2023-06-30 07:31:41
言語 Ruby
(3.3.0)
結果
AC  
実行時間 2,914 ms / 4,000 ms
コード長 897 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 11,448 KB
実行使用メモリ 74,804 KB
最終ジャッジ日時 2023-09-21 05:22:41
合計ジャッジ時間 56,873 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,148 KB
testcase_01 AC 81 ms
15,304 KB
testcase_02 AC 82 ms
15,112 KB
testcase_03 AC 81 ms
15,068 KB
testcase_04 AC 82 ms
15,204 KB
testcase_05 AC 80 ms
15,120 KB
testcase_06 AC 81 ms
15,048 KB
testcase_07 AC 81 ms
15,240 KB
testcase_08 AC 2,774 ms
74,632 KB
testcase_09 AC 2,806 ms
74,560 KB
testcase_10 AC 2,814 ms
74,660 KB
testcase_11 AC 2,785 ms
74,600 KB
testcase_12 AC 2,771 ms
74,580 KB
testcase_13 AC 2,818 ms
74,580 KB
testcase_14 AC 2,581 ms
73,260 KB
testcase_15 AC 2,716 ms
74,036 KB
testcase_16 AC 2,649 ms
74,256 KB
testcase_17 AC 2,715 ms
73,404 KB
testcase_18 AC 88 ms
15,264 KB
testcase_19 AC 87 ms
15,244 KB
testcase_20 AC 88 ms
15,148 KB
testcase_21 AC 86 ms
15,256 KB
testcase_22 AC 87 ms
15,204 KB
testcase_23 AC 2,914 ms
73,888 KB
testcase_24 AC 2,732 ms
73,896 KB
testcase_25 AC 2,829 ms
74,804 KB
testcase_26 AC 1,851 ms
71,632 KB
testcase_27 AC 1,857 ms
71,516 KB
testcase_28 AC 1,858 ms
71,632 KB
testcase_29 AC 1,822 ms
73,168 KB
testcase_30 AC 1,919 ms
73,264 KB
testcase_31 AC 1,826 ms
73,080 KB
testcase_32 AC 1,965 ms
71,580 KB
testcase_33 AC 1,982 ms
71,676 KB
testcase_34 AC 1,981 ms
71,524 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

MOD = 998244353
n = gets.to_i
g = Array.new(n) { [] }
(n - 1).times do
  a, b = gets.split.map(&:to_i)
  g[a - 1] << b - 1
  g[b - 1] << a - 1
end
seen = Array.new(n, false)
seen[0] = true
postorder = []
child = Array.new(n) { [] }
stk = [~0, 0]
while !stk.empty?
  u = stk.pop
  if u >= 0
    g[u].each do |v|
      next if seen[v]
      seen[v] = true
      child[u] << v
      stk.push(~v, v)
    end
  else
    postorder << ~u
  end
end
a = gets.split.map(&:to_i)
ans = 0
30.times do |k|
  dp0 = Array.new(n, 0)
  dp1 = Array.new(n, 0)
  postorder.each do |u|
    if (a[u] >> k & 1) == 1
      dp1[u] = 1
    else
      dp0[u] = 1
    end
    child[u].each do |v|
      ndp0 = dp0[u]*dp0[v] + dp1[u]*dp1[v] + dp0[u]*dp1[v]
      ndp1 = dp0[u]*dp1[v] + dp1[u]*dp0[v] + dp1[u]*dp1[v]
      dp0[u] = ndp0 % MOD
      dp1[u] = ndp1 % MOD
    end
  end
  ans += dp1[0] << k
end
ans %= MOD
puts ans
0