結果

問題 No.1207 グラフX
ユーザー AEnAEn
提出日時 2022-12-10 16:06:49
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,899 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 82,760 KB
実行使用メモリ 377,660 KB
最終ジャッジ日時 2024-04-22 23:57:59
合計ジャッジ時間 41,108 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 854 ms
126,820 KB
testcase_01 AC 867 ms
126,720 KB
testcase_02 AC 871 ms
126,592 KB
testcase_03 AC 870 ms
126,976 KB
testcase_04 AC 851 ms
126,720 KB
testcase_05 AC 1,663 ms
374,656 KB
testcase_06 TLE -
testcase_07 AC 1,986 ms
376,604 KB
testcase_08 AC 696 ms
105,448 KB
testcase_09 AC 769 ms
115,048 KB
testcase_10 AC 1,679 ms
304,128 KB
testcase_11 AC 1,670 ms
375,624 KB
testcase_12 AC 747 ms
108,724 KB
testcase_13 AC 362 ms
85,124 KB
testcase_14 AC 845 ms
125,124 KB
testcase_15 AC 736 ms
112,616 KB
testcase_16 AC 381 ms
86,752 KB
testcase_17 AC 595 ms
102,936 KB
testcase_18 AC 533 ms
105,180 KB
testcase_19 AC 519 ms
92,544 KB
testcase_20 AC 863 ms
126,212 KB
testcase_21 AC 148 ms
78,836 KB
testcase_22 AC 605 ms
104,292 KB
testcase_23 AC 676 ms
109,312 KB
testcase_24 AC 513 ms
101,504 KB
testcase_25 AC 916 ms
125,440 KB
testcase_26 AC 735 ms
112,640 KB
testcase_27 AC 841 ms
119,184 KB
testcase_28 AC 792 ms
114,776 KB
testcase_29 AC 819 ms
120,132 KB
testcase_30 AC 460 ms
92,928 KB
testcase_31 AC 350 ms
82,756 KB
testcase_32 AC 424 ms
94,848 KB
testcase_33 AC 439 ms
93,772 KB
testcase_34 AC 773 ms
111,872 KB
testcase_35 AC 172 ms
79,616 KB
testcase_36 AC 777 ms
114,092 KB
testcase_37 AC 690 ms
105,852 KB
testcase_38 AC 269 ms
84,608 KB
testcase_39 AC 462 ms
95,872 KB
testcase_40 AC 282 ms
79,616 KB
testcase_41 AC 538 ms
93,184 KB
testcase_42 AC 77 ms
67,456 KB
testcase_43 AC 77 ms
67,200 KB
testcase_44 AC 78 ms
67,200 KB
testcase_45 AC 864 ms
126,592 KB
testcase_46 AC 878 ms
126,976 KB
testcase_47 AC 876 ms
127,232 KB
testcase_48 AC 877 ms
126,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
import pypyjit
pypyjit.set_param("max_unroll_recursion=-1")

class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

        if self.parent[x] > self.parent[y]:
            x, y = y, x

        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_count

N, M, X = map(int, input().split())
uf = UnionFind(N)
G = [list() for _ in range(N)]
for i in range(M):
    x, y, z = map(int, input().split())
    x-=1;y-=1
    if uf.is_same(x,y):continue
    uf.unite(x,y)
    G[x].append((y,z))
    G[y].append((x,z))

sub_tree = [0]*N
res = 0
mod = 10**9+7
def dfs(v, p):
    for next_v,_ in G[v]:
        if next_v==p:
            continue
        dfs(next_v, v)
    sub_tree[v] = 1
    for c,cost in G[v]:
        if c==p:
            continue
        sub_tree[v] += sub_tree[c]
        global res
        res += (pow(X,cost,mod)*(sub_tree[c]*(N-sub_tree[c])))%mod
        res %= mod

dfs(0,-1)
print(res)
0