結果

問題 No.1843 Tree ANDistance
ユーザー moharan627moharan627
提出日時 2022-02-18 22:49:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,973 ms / 2,000 ms
コード長 2,832 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 131,836 KB
最終ジャッジ日時 2024-06-29 09:37:13
合計ジャッジ時間 31,737 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,712 ms
127,276 KB
testcase_01 AC 1,677 ms
131,236 KB
testcase_02 AC 1,690 ms
131,836 KB
testcase_03 AC 1,916 ms
131,756 KB
testcase_04 AC 1,973 ms
127,220 KB
testcase_05 AC 1,306 ms
128,800 KB
testcase_06 AC 1,284 ms
127,724 KB
testcase_07 AC 1,594 ms
125,296 KB
testcase_08 AC 1,653 ms
130,588 KB
testcase_09 AC 1,504 ms
122,916 KB
testcase_10 AC 1,539 ms
123,748 KB
testcase_11 AC 1,575 ms
126,632 KB
testcase_12 AC 1,172 ms
125,060 KB
testcase_13 AC 1,325 ms
125,640 KB
testcase_14 AC 114 ms
77,440 KB
testcase_15 AC 128 ms
77,760 KB
testcase_16 AC 96 ms
77,440 KB
testcase_17 AC 100 ms
77,184 KB
testcase_18 AC 96 ms
76,704 KB
testcase_19 AC 120 ms
77,520 KB
testcase_20 AC 95 ms
76,928 KB
testcase_21 AC 42 ms
54,912 KB
testcase_22 AC 44 ms
54,528 KB
testcase_23 AC 43 ms
54,656 KB
testcase_24 AC 43 ms
54,912 KB
testcase_25 AC 44 ms
54,656 KB
testcase_26 AC 45 ms
54,656 KB
testcase_27 AC 44 ms
54,528 KB
testcase_28 AC 1,142 ms
111,488 KB
testcase_29 AC 451 ms
99,104 KB
testcase_30 AC 613 ms
98,448 KB
testcase_31 AC 1,064 ms
118,116 KB
testcase_32 AC 1,071 ms
113,032 KB
testcase_33 AC 260 ms
90,616 KB
testcase_34 AC 587 ms
103,636 KB
testcase_35 AC 42 ms
54,316 KB
testcase_36 AC 42 ms
54,912 KB
testcase_37 AC 43 ms
54,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#sys.setrecursionlimit(10 ** 6)
INF = float('inf')
#10**20,2**63に変えるのもあり
MOD = 10**9 + 7
MOD2 = 998244353
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):
        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 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())
def solve():
    def II(): return int(sys.stdin.readline())
    def LI(): return list(map(int, sys.stdin.readline().split()))
    def LC(): return list(input())
    def IC(): return [int(c) for c in input()]
    def MI(): return map(int, sys.stdin.readline().split())
    N = II()
    Graph = [[] for _ in range(N)]
    for _ in range(N-1):
        A,B,C =MI()
        A-=1
        B-=1
        Graph[A].append((B,C))
        Graph[B].append((A,C))
    from collections import deque
    Ans = 0
    for bit in range(30):
        Q = deque()
        Seen = [False for i in range(N)]
        uf = UnionFind(N)
        for u in range(N):
            if (Seen[u] == True):  # 始点sが探索済みなら
                continue
            else:
                Q.append(u)
                # 始点をキューに入れる
                while Q:
                    s = Q.pop()
                    Seen[s] = True
                    for t,c in Graph[s]:
                        if Seen[t] == False and c & (1 << bit):
                            uf.union(s,t)
                            Q.append(t)
        Dec = [0]*N
        #print(uf.all_group_members())
        Num = pow(2,bit,MOD)
        for n in range(N):
            find = uf.find(n)
            Ans += (uf.size(n)-1-Dec[find]) * Num
            Dec[find] += 1
            Ans %= MOD
        #print(bit,Ans)
    print(Ans % MOD)
    return
solve()
0