結果
問題 | No.1843 Tree ANDistance |
ユーザー |
![]() |
提出日時 | 2022-02-18 22:08:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 819 ms / 2,000 ms |
コード長 | 963 bytes |
コンパイル時間 | 315 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 139,356 KB |
最終ジャッジ日時 | 2024-06-29 09:02:33 |
合計ジャッジ時間 | 15,409 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 38 |
ソースコード
import sys input = sys.stdin.readline mod=10**9+7 n=int(input()) # UnionFind def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): if Nodes[find(x)] < Nodes[find(y)]: Nodes[find(y)] += Nodes[find(x)] Nodes[find(x)] = 0 Group[find(x)] = find(y) else: Nodes[find(x)] += Nodes[find(y)] Nodes[find(y)] = 0 Group[find(y)] = find(x) E=[tuple(map(int,input().split())) for i in range(n-1)] LANS=0 for i in range(32): Group = [i for i in range(n+1)] # グループ分け Nodes = [1]*(n+1) # 各グループのノードの数 for x,y,c in E: x-=1 y-=1 if c & (1<<i) != 0: Union(x,y) ANS=0 for j in range(n): if find(j)==j: x=Nodes[j] ANS+=x*(x-1)//2 LANS+=ANS*(1<<i) print(LANS%mod)