結果

問題 No.1207 グラフX
ユーザー tentententen
提出日時 2020-12-04 08:48:03
言語 Java19
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,313 bytes
コンパイル時間 4,417 ms
コンパイル使用メモリ 80,972 KB
実行使用メモリ 267,904 KB
最終ジャッジ日時 2023-10-12 20:42:46
合計ジャッジ時間 19,379 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static final int MOD = 1000000007;
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static int[] sizes;
    static int n;
    static int x;
    static long sum = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int m = sc.nextInt();
        x = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new HashMap<>());
        }
        UnionFindTree uft = new UnionFindTree(n);
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            if (!uft.same(a, b)) {
                uft.unite(a, b);
                graph.get(a).put(b, c);
                graph.get(b).put(a, c);
            }
        }
        sizes = new int[n];
        getSize(0, 0);
        System.out.println(sum);
    }
    
    static int getSize(int idx, int parent) {
        sizes[idx] = 1;
        for (int y : graph.get(idx).keySet()) {
            if (y == parent) {
                continue;
            }
            int s = getSize(y, idx);
            sum += pow(x, graph.get(idx).get(y)) * (n - s) % MOD * s % MOD;
            sum %= MOD;
            sizes[idx] += s;
        }
        return sizes[idx];
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return x * pow(x, p - 1) % MOD;
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int a) {
            if (parents[a] == a) {
                return a;
            } else {
                return parents[a] = find(parents[a]);
            }
        }
        
        public boolean same(int a, int b) {
            return find(a) == find(b);
        }
        
        public void unite(int a, int b) {
            if (!same(a, b)) {
                parents[find(a)] = find(b);
            }
        }
    }
}
0