結果

問題 No.1843 Tree ANDistance
ユーザー ygd.ygd.
提出日時 2022-02-18 21:49:13
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,597 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 229,664 KB
最終ジャッジ日時 2023-09-11 18:57:16
合計ジャッジ時間 42,194 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 AC 1,922 ms
197,460 KB
testcase_05 AC 1,362 ms
229,664 KB
testcase_06 AC 1,720 ms
225,452 KB
testcase_07 AC 1,891 ms
197,816 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 AC 1,301 ms
204,552 KB
testcase_13 AC 1,713 ms
197,896 KB
testcase_14 AC 154 ms
78,536 KB
testcase_15 AC 151 ms
78,020 KB
testcase_16 AC 136 ms
78,000 KB
testcase_17 AC 130 ms
77,892 KB
testcase_18 AC 120 ms
77,652 KB
testcase_19 AC 144 ms
78,428 KB
testcase_20 AC 127 ms
78,360 KB
testcase_21 AC 73 ms
71,384 KB
testcase_22 AC 74 ms
71,108 KB
testcase_23 AC 75 ms
71,388 KB
testcase_24 AC 73 ms
71,228 KB
testcase_25 AC 70 ms
71,216 KB
testcase_26 AC 71 ms
71,132 KB
testcase_27 AC 73 ms
71,240 KB
testcase_28 AC 1,335 ms
178,308 KB
testcase_29 AC 1,045 ms
156,008 KB
testcase_30 AC 1,047 ms
148,936 KB
testcase_31 AC 1,554 ms
197,112 KB
testcase_32 AC 1,532 ms
182,856 KB
testcase_33 AC 420 ms
110,768 KB
testcase_34 AC 1,023 ms
174,152 KB
testcase_35 AC 68 ms
71,184 KB
testcase_36 AC 74 ms
71,228 KB
testcase_37 AC 73 ms
71,384 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 = set([])
        for j in range(N):
            if uf.find(j) in used: continue
            used.add(uf.find(j))
            #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