結果

問題 No.1843 Tree ANDistance
ユーザー 👑 tamatotamato
提出日時 2022-02-18 21:26:10
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 842 ms / 2,000 ms
コード長 1,495 bytes
コンパイル時間 1,444 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 140,256 KB
最終ジャッジ日時 2023-09-11 18:30:51
合計ジャッジ時間 18,561 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 842 ms
140,108 KB
testcase_01 AC 808 ms
139,940 KB
testcase_02 AC 820 ms
140,256 KB
testcase_03 AC 817 ms
140,104 KB
testcase_04 AC 842 ms
140,184 KB
testcase_05 AC 699 ms
139,944 KB
testcase_06 AC 781 ms
139,980 KB
testcase_07 AC 780 ms
137,064 KB
testcase_08 AC 786 ms
138,588 KB
testcase_09 AC 785 ms
136,900 KB
testcase_10 AC 760 ms
135,736 KB
testcase_11 AC 815 ms
139,668 KB
testcase_12 AC 565 ms
132,252 KB
testcase_13 AC 707 ms
135,076 KB
testcase_14 AC 143 ms
78,176 KB
testcase_15 AC 145 ms
78,728 KB
testcase_16 AC 126 ms
78,408 KB
testcase_17 AC 124 ms
77,972 KB
testcase_18 AC 119 ms
78,088 KB
testcase_19 AC 119 ms
78,368 KB
testcase_20 AC 127 ms
78,264 KB
testcase_21 AC 74 ms
71,836 KB
testcase_22 AC 74 ms
71,440 KB
testcase_23 AC 75 ms
71,592 KB
testcase_24 AC 76 ms
71,752 KB
testcase_25 AC 74 ms
71,760 KB
testcase_26 AC 73 ms
71,836 KB
testcase_27 AC 75 ms
71,548 KB
testcase_28 AC 513 ms
105,712 KB
testcase_29 AC 394 ms
99,848 KB
testcase_30 AC 415 ms
98,332 KB
testcase_31 AC 564 ms
130,432 KB
testcase_32 AC 536 ms
126,292 KB
testcase_33 AC 239 ms
88,488 KB
testcase_34 AC 415 ms
102,480 KB
testcase_35 AC 74 ms
71,768 KB
testcase_36 AC 75 ms
71,748 KB
testcase_37 AC 75 ms
71,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.root = [-1] * (n + 1)
            self.rnk = [0] * (n + 1)

        def find_root(self, x):
            while self.root[x] >= 0:
                x = self.root[x]
            return x

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

        def size(self, x):
            return -self.root[self.find_root(x)]

    N = int(input())
    ABC = []
    for _ in range(N - 1):
        ABC.append(tuple(map(int, input().split())))

    ans = 0
    for lv in range(31):
        UF = UnionFind(N)
        for a, b, c in ABC:
            if c >> lv & 1:
                UF.unite(a, b)
        for v in range(1, N+1):
            if UF.find_root(v) == v:
                x = UF.size(v)
                ans += pow(2, lv, mod) * x * (x - 1) // 2
                ans %= mod
    print(ans)


if __name__ == '__main__':
    main()
0