結果

問題 No.1843 Tree ANDistance
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-01-15 19:27:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,026 ms / 2,000 ms
コード長 1,743 bytes
コンパイル時間 732 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 227,852 KB
最終ジャッジ日時 2023-08-28 12:00:26
合計ジャッジ時間 26,058 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,024 ms
204,596 KB
testcase_01 AC 979 ms
190,056 KB
testcase_02 AC 930 ms
199,796 KB
testcase_03 AC 927 ms
192,316 KB
testcase_04 AC 1,005 ms
202,188 KB
testcase_05 AC 753 ms
199,912 KB
testcase_06 AC 859 ms
227,852 KB
testcase_07 AC 964 ms
196,872 KB
testcase_08 AC 936 ms
193,232 KB
testcase_09 AC 829 ms
172,220 KB
testcase_10 AC 837 ms
174,816 KB
testcase_11 AC 1,026 ms
205,568 KB
testcase_12 AC 725 ms
202,132 KB
testcase_13 AC 790 ms
203,632 KB
testcase_14 AC 238 ms
81,812 KB
testcase_15 AC 235 ms
82,012 KB
testcase_16 AC 189 ms
81,192 KB
testcase_17 AC 189 ms
81,512 KB
testcase_18 AC 172 ms
80,256 KB
testcase_19 AC 184 ms
81,872 KB
testcase_20 AC 186 ms
81,124 KB
testcase_21 AC 112 ms
72,772 KB
testcase_22 AC 115 ms
72,824 KB
testcase_23 AC 113 ms
72,960 KB
testcase_24 AC 108 ms
72,864 KB
testcase_25 AC 110 ms
72,988 KB
testcase_26 AC 112 ms
72,768 KB
testcase_27 AC 113 ms
72,956 KB
testcase_28 AC 676 ms
199,232 KB
testcase_29 AC 525 ms
158,036 KB
testcase_30 AC 519 ms
150,692 KB
testcase_31 AC 690 ms
218,540 KB
testcase_32 AC 688 ms
212,084 KB
testcase_33 AC 264 ms
107,472 KB
testcase_34 AC 545 ms
171,780 KB
testcase_35 AC 112 ms
72,744 KB
testcase_36 AC 112 ms
72,696 KB
testcase_37 AC 114 ms
73,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from heapq import *
from functools import *
import sys,math
input = sys.stdin.readline

N = int(input())
edge = defaultdict(int)
for _ in range(N-1):
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    edge[(a,b)]=c

mod = 10**9 + 7
class DSU:
    def __init__(self, n):
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        x, y = self.leader(a), self.leader(b)
        if x == y: return x
        if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return x

    def same(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        assert 0 <= a < self._n
        if self.parent_or_size[a] < 0: return a
        self.parent_or_size[a] = self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]

    def size(self, a):
        assert 0 <= a < self._n
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        leader_buf = [self.leader(i) for i in range(self._n)]
        result = [[] for _ in range(self._n)]
        for i in range(self._n): result[leader_buf[i]].append(i)
        return [r for r in result if r != []]

ans = 0

for i in range(31):
    
    D = DSU(N)
    
    for k,v in edge.items():
        
        if (v>>i)&1:
            D.merge(k[0],k[1])
    B = (1<<i)
    for g in D.groups():
        
        n = len(g)
        
        ans += B*(n*(n-1))//2
        ans %= mod
print(ans)
    
    
    
    
0