結果

問題 No.1843 Tree ANDistance
ユーザー ygd.ygd.
提出日時 2022-02-18 21:52:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,836 ms / 2,000 ms
コード長 2,598 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 179,660 KB
最終ジャッジ日時 2023-09-11 19:01:16
合計ジャッジ時間 32,574 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,834 ms
144,408 KB
testcase_01 AC 1,733 ms
144,696 KB
testcase_02 AC 1,641 ms
143,804 KB
testcase_03 AC 1,836 ms
152,272 KB
testcase_04 AC 1,497 ms
144,376 KB
testcase_05 AC 1,095 ms
179,660 KB
testcase_06 AC 1,303 ms
174,204 KB
testcase_07 AC 1,496 ms
140,532 KB
testcase_08 AC 1,723 ms
147,700 KB
testcase_09 AC 1,741 ms
140,624 KB
testcase_10 AC 1,563 ms
142,064 KB
testcase_11 AC 1,784 ms
151,448 KB
testcase_12 AC 1,025 ms
164,632 KB
testcase_13 AC 1,359 ms
156,764 KB
testcase_14 AC 142 ms
78,356 KB
testcase_15 AC 145 ms
78,132 KB
testcase_16 AC 127 ms
77,968 KB
testcase_17 AC 124 ms
77,896 KB
testcase_18 AC 113 ms
77,164 KB
testcase_19 AC 134 ms
78,508 KB
testcase_20 AC 119 ms
78,184 KB
testcase_21 AC 70 ms
71,276 KB
testcase_22 AC 69 ms
71,280 KB
testcase_23 AC 72 ms
70,928 KB
testcase_24 AC 71 ms
71,108 KB
testcase_25 AC 71 ms
71,104 KB
testcase_26 AC 71 ms
71,372 KB
testcase_27 AC 73 ms
71,248 KB
testcase_28 AC 989 ms
126,188 KB
testcase_29 AC 722 ms
108,512 KB
testcase_30 AC 682 ms
110,896 KB
testcase_31 AC 970 ms
133,960 KB
testcase_32 AC 987 ms
130,764 KB
testcase_33 AC 312 ms
97,360 KB
testcase_34 AC 654 ms
132,356 KB
testcase_35 AC 71 ms
71,464 KB
testcase_36 AC 71 ms
71,308 KB
testcase_37 AC 71 ms
71,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline #文字列につけてはダメ
input = sys.stdin.buffer.readline #文字列につけてはダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]


def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N-1):
        a,b,c = map(int,input().split())
        a -= 1; b -= 1
        G[a].append((b,c)); G[b].append((a,c))
    
    ans = 0
    for i in range(33):
        base = (1<<i) #この桁を見ている。
        uf = UnionFind(N)
        stack = []
        stack.append(0)
        par = [-1]*N
        while stack:
            v = stack.pop()
            for u,c in G[v]:
                if par[v] == u: continue
                par[u] = v
                if (c>>i)&1 == 1:
                    uf.union(u,v)
                stack.append(u)
        used = [0]*N
        for j in range(N):
            if used[uf.find(j)] == 1: continue
            used[uf.find(j)] = 1
            #print(j,uf.find(j),uf.get_size(j))
            num = uf.get_size(j)
            temp = num*(num-1)//2%MOD
            ans += base*temp%MOD
            ans %= MOD
    print(ans)


        
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
    def find(self, x):
        """
        x が属するグループを探索して親を出す。
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]
    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)
    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]


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