結果

問題 No.1843 Tree ANDistance
ユーザー horitaka1999horitaka1999
提出日時 2022-02-18 23:13:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 567 ms / 2,000 ms
コード長 1,938 bytes
コンパイル時間 592 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 138,956 KB
最終ジャッジ日時 2023-09-11 20:04:38
合計ジャッジ時間 14,822 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 559 ms
138,956 KB
testcase_01 AC 558 ms
138,588 KB
testcase_02 AC 520 ms
135,804 KB
testcase_03 AC 505 ms
130,040 KB
testcase_04 AC 564 ms
129,608 KB
testcase_05 AC 431 ms
131,412 KB
testcase_06 AC 474 ms
137,872 KB
testcase_07 AC 537 ms
135,500 KB
testcase_08 AC 548 ms
137,492 KB
testcase_09 AC 511 ms
125,412 KB
testcase_10 AC 503 ms
135,152 KB
testcase_11 AC 567 ms
135,412 KB
testcase_12 AC 398 ms
133,644 KB
testcase_13 AC 451 ms
133,952 KB
testcase_14 AC 202 ms
79,568 KB
testcase_15 AC 202 ms
79,560 KB
testcase_16 AC 171 ms
78,672 KB
testcase_17 AC 169 ms
78,448 KB
testcase_18 AC 141 ms
77,420 KB
testcase_19 AC 160 ms
79,408 KB
testcase_20 AC 167 ms
78,460 KB
testcase_21 AC 94 ms
71,736 KB
testcase_22 AC 95 ms
71,712 KB
testcase_23 AC 96 ms
71,488 KB
testcase_24 AC 95 ms
71,536 KB
testcase_25 AC 95 ms
71,536 KB
testcase_26 AC 94 ms
71,776 KB
testcase_27 AC 94 ms
71,600 KB
testcase_28 AC 387 ms
124,732 KB
testcase_29 AC 322 ms
110,264 KB
testcase_30 AC 326 ms
111,564 KB
testcase_31 AC 393 ms
129,468 KB
testcase_32 AC 402 ms
125,956 KB
testcase_33 AC 183 ms
93,132 KB
testcase_34 AC 307 ms
116,656 KB
testcase_35 AC 96 ms
71,488 KB
testcase_36 AC 96 ms
71,692 KB
testcase_37 AC 98 ms
71,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):#self.parentsに要素数を記録しているのでほぼO(1)でできる
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):#o(N)かかって非常に遅いです
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

N = int(input())
ans = 0
store = []
mod = 10 ** 9 + 7
for _ in range(N-1):
    a,b,c = map(int,input().split())
    a-=1
    b-=1
    store.append((a,b,c))
for k in range(30):
    uf = UnionFind(N)
    cnt = [0 for _ in range(N)]
    s = 0
    for a,b,c in store:
        if (c>>k) & 1:
            uf.union(a,b)
    for i in range(N):
        cnt[uf.find(i)] += 1
    for i in range(N):
        if cnt[i] > 1:
            s += (cnt[i] * (cnt[i]-1)) //2
            s %= mod
    ans += pow(2,k,mod) * s
    ans %= mod
print(ans)
    
0