結果

問題 No.1843 Tree ANDistance
ユーザー sasa8uyauya
提出日時 2025-04-04 20:41:55
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 600 bytes
コンパイル時間 355 ms
コンパイル使用メモリ 82,852 KB
実行使用メモリ 119,744 KB
最終ジャッジ日時 2025-04-04 20:42:03
合計ジャッジ時間 7,562 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1 -- * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())
e=[[] for i in range(n)]
for _ in range(n-1):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  e[a]+=[(b,c)]
  e[b]+=[(a,c)]
M=10**9+7
i2=pow(2,M-2,M)
ans=0
v=[0]*n
u=[0]*n
for i in range(30):
  a=0
  q=[0]
  while len(q)>0:
    s=q[-1]
    if v[s]==0:
      v[s]=1
      q+=[t for t,c in e[s] if v[t]==0]
    else:
      u[s]=0
      u[s]=sum(u[t]+1 for t,c in e[s] if v[t]==0 and (c>>i)&1==1)
      a+=u[s]*2
      a+=u[s]*u[s]
      a-=sum((u[t]+1)*(u[t]+1) for t,c in e[s] if v[t]==0 and (c>>i)&1==1)
      a%=M
      v[s]=0
      q.pop()
  ans+=a*i2*pow(2,i,M)
  ans%=M
print(ans)
0