結果

問題 No.1843 Tree ANDistance
ユーザー ygd.ygd.
提出日時 2022-02-18 21:46:23
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,573 bytes
コンパイル時間 422 ms
コンパイル使用メモリ 86,884 KB
実行使用メモリ 220,404 KB
最終ジャッジ日時 2023-09-11 18:53:34
合計ジャッジ時間 42,048 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 74 ms
71,276 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 73 ms
70,984 KB
testcase_26 AC 74 ms
71,152 KB
testcase_27 WA -
testcase_28 AC 1,310 ms
178,396 KB
testcase_29 AC 1,114 ms
156,316 KB
testcase_30 AC 1,115 ms
154,336 KB
testcase_31 AC 1,657 ms
197,372 KB
testcase_32 AC 1,578 ms
183,052 KB
testcase_33 AC 423 ms
114,260 KB
testcase_34 AC 1,056 ms
176,972 KB
testcase_35 AC 76 ms
71,320 KB
testcase_36 AC 75 ms
71,320 KB
testcase_37 AC 77 ms
71,184 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)%MOD #この桁を見ている。
        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)
            ans += base*num%MOD*(num-1)//2
            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