結果
| 問題 |
No.2377 SUM AND XOR on Tree
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2023-07-13 04:45:00 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,401 bytes |
| コンパイル時間 | 466 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 296,028 KB |
| 最終ジャッジ日時 | 2024-09-14 16:51:15 |
| 合計ジャッジ時間 | 50,005 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 32 TLE * 1 |
ソースコード
N=int(input())
E=[[] for i in range(N)]
for i in range(N-1):
x,y=map(int,input().split())
x-=1
y-=1
E[x].append(y)
E[y].append(x)
A=list(map(int,input().split()))
ROOT=0
QUE=[ROOT]
Parent=[-1]*N
Parent[ROOT]=N # ROOTの親を定めておく.
Child=[[] for i in range(N)]
TOP_SORT=[] # トポロジカルソート
while QUE: # トポロジカルソートと同時に親を見つける
x=QUE.pop()
TOP_SORT.append(x)
for to in E[x]:
if Parent[to]==-1:
Parent[to]=x
Child[x].append(to)
QUE.append(to)
ANS=0
mod=998244353
for i in range(30):
X=[0]*N
for j in range(N):
if A[j] & (1<<i) != 0:
X[j]=1
DP=[[0,0] for i in range(N+1)]
for x in TOP_SORT[::-1]:
if len(Child[x])==0:
if X[x]==1:
DP[x]=[0,1]
else:
DP[x]=[1,0]
else:
NOW=[-1,-1]
for c in Child[x]:
if NOW==[-1,-1]:
NOW=DP[c]
else:
b = NOW[0]*DP[c][1] + NOW[1]*DP[c][0]
a = NOW[0]*DP[c][0] + NOW[1]*DP[c][1]
NOW=[a%mod,b%mod]
DP[x]=NOW
if X[x]==1:
DP[x][0],DP[x][1]=DP[x][1],DP[x][0]
DP[x][0]+=DP[x][1]
ANS+=DP[0][1]*(1<<i)
print(ANS%mod)
titia