結果

問題 No.1207 グラフX
ユーザー tentententen
提出日時 2020-12-04 09:25:15
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,556 bytes
コンパイル時間 4,052 ms
コンパイル使用メモリ 76,136 KB
実行使用メモリ 161,600 KB
最終ジャッジ日時 2023-10-12 21:34:48
合計ジャッジ時間 23,922 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,356 ms
144,384 KB
testcase_01 AC 1,394 ms
147,520 KB
testcase_02 AC 1,265 ms
147,828 KB
testcase_03 AC 1,274 ms
148,916 KB
testcase_04 AC 1,323 ms
146,828 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 1,133 ms
117,048 KB
testcase_09 AC 1,236 ms
121,244 KB
testcase_10 AC 1,771 ms
150,116 KB
testcase_11 TLE -
testcase_12 AC 1,228 ms
119,344 KB
testcase_13 AC 720 ms
75,812 KB
testcase_14 AC 1,357 ms
140,736 KB
testcase_15 AC 1,267 ms
126,532 KB
testcase_16 AC 795 ms
80,200 KB
testcase_17 AC 1,058 ms
104,756 KB
testcase_18 AC 1,122 ms
104,000 KB
testcase_19 AC 914 ms
94,092 KB
testcase_20 AC 1,287 ms
141,716 KB
testcase_21 AC 263 ms
59,448 KB
testcase_22 AC 1,153 ms
101,996 KB
testcase_23 AC 1,135 ms
117,452 KB
testcase_24 AC 941 ms
105,312 KB
testcase_25 AC 1,343 ms
141,388 KB
testcase_26 AC 1,154 ms
119,876 KB
testcase_27 AC 1,248 ms
125,588 KB
testcase_28 AC 1,240 ms
121,220 KB
testcase_29 AC 1,225 ms
134,548 KB
testcase_30 AC 799 ms
90,000 KB
testcase_31 AC 563 ms
67,896 KB
testcase_32 AC 749 ms
87,292 KB
testcase_33 AC 777 ms
92,600 KB
testcase_34 AC 1,137 ms
118,788 KB
testcase_35 AC 274 ms
60,280 KB
testcase_36 AC 1,141 ms
121,000 KB
testcase_37 AC 1,082 ms
107,480 KB
testcase_38 AC 452 ms
67,272 KB
testcase_39 AC 757 ms
93,612 KB
testcase_40 AC 476 ms
61,304 KB
testcase_41 AC 897 ms
93,672 KB
testcase_42 AC 117 ms
55,416 KB
testcase_43 AC 117 ms
55,896 KB
testcase_44 AC 118 ms
55,848 KB
testcase_45 AC 1,204 ms
142,928 KB
testcase_46 AC 1,205 ms
141,456 KB
testcase_47 AC 1,230 ms
141,716 KB
testcase_48 AC 1,196 ms
141,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static final int MOD = 1000000007;
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static int n;
    static int x;
    static long sum = 0;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 3);
        n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        x = Integer.parseInt(first[2]);
        for (int i = 0; i < n; i++) {
            graph.add(new HashMap<>());
        }
        UnionFindTree uft = new UnionFindTree(n);
        for (int i = 0; i < m; i++) {
            String[] line = br.readLine().split(" ", 3);
            int a = Integer.parseInt(line[0]) - 1;
            int b = Integer.parseInt(line[1]) - 1;
            int c = Integer.parseInt(line[2]);
            if (!uft.same(a, b)) {
                uft.unite(a, b);
                graph.get(a).put(b, c);
                graph.get(b).put(a, c);
            }
        }
        getSize(0, 0);
        System.out.println(sum);
    }
    
    static int getSize(int idx, int parent) {
        int size = 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;
            size += s;
        }
        return size;
    }
    
    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