結果

問題 No.1843 Tree ANDistance
ユーザー ch1channnch1channn
提出日時 2023-03-03 03:06:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 504 ms / 2,000 ms
コード長 2,033 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 81,740 KB
実行使用メモリ 210,932 KB
最終ジャッジ日時 2023-10-17 22:03:04
合計ジャッジ時間 14,707 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 488 ms
118,868 KB
testcase_01 AC 496 ms
118,156 KB
testcase_02 AC 456 ms
120,572 KB
testcase_03 AC 451 ms
120,280 KB
testcase_04 AC 499 ms
119,052 KB
testcase_05 AC 400 ms
119,956 KB
testcase_06 AC 439 ms
119,272 KB
testcase_07 AC 470 ms
116,612 KB
testcase_08 AC 473 ms
117,504 KB
testcase_09 AC 448 ms
119,720 KB
testcase_10 AC 427 ms
118,476 KB
testcase_11 AC 504 ms
119,168 KB
testcase_12 AC 366 ms
119,236 KB
testcase_13 AC 429 ms
120,532 KB
testcase_14 AC 139 ms
77,480 KB
testcase_15 AC 143 ms
77,552 KB
testcase_16 AC 109 ms
76,480 KB
testcase_17 AC 108 ms
76,648 KB
testcase_18 AC 88 ms
75,840 KB
testcase_19 AC 107 ms
76,696 KB
testcase_20 AC 106 ms
76,336 KB
testcase_21 AC 38 ms
53,444 KB
testcase_22 AC 38 ms
53,444 KB
testcase_23 AC 39 ms
53,444 KB
testcase_24 AC 39 ms
53,444 KB
testcase_25 AC 38 ms
53,444 KB
testcase_26 AC 38 ms
53,444 KB
testcase_27 AC 39 ms
53,444 KB
testcase_28 AC 376 ms
125,104 KB
testcase_29 AC 306 ms
112,780 KB
testcase_30 AC 314 ms
113,208 KB
testcase_31 AC 411 ms
210,932 KB
testcase_32 AC 400 ms
194,140 KB
testcase_33 AC 149 ms
92,416 KB
testcase_34 AC 299 ms
117,520 KB
testcase_35 AC 39 ms
53,444 KB
testcase_36 AC 39 ms
53,444 KB
testcase_37 AC 40 ms
53,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
if sys.platform =='ios':
	import clipboard
	a=clipboard.get()
	a = a.split('\n')
	text = '\n'.join(a)
	with open('input_file.txt','w') as f:
		f.write(text)
	sys.stdin = open('input_file.txt')
sys.setrecursionlimit(4100000)
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()
nm = lambda: map(int,input().split())


MOD = 10 ** 9 + 7

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = 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
        self.group -= 1
        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):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        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 self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


n = int(input())
abc = [list(map(int, input().split())) for _ in range(n - 1)]
ans = 0
bi = 1
for i in range(30):
    UF = UnionFind(n)
    for a, b, c in abc:
        if c >> i & 1:
            UF.union(a - 1, b - 1)
    for r in UF.roots():
        r = UF.size(r)
        ans += r * (r - 1) // 2 * bi
        ans %= MOD
    bi *= 2
print(ans)
0