結果

問題 No.1207 グラフX
ユーザー tentententen
提出日時 2023-03-17 17:49:01
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,481 bytes
コンパイル時間 2,861 ms
コンパイル使用メモリ 86,748 KB
実行使用メモリ 154,204 KB
最終ジャッジ日時 2023-10-18 13:46:53
合計ジャッジ時間 64,811 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,306 ms
132,264 KB
testcase_01 AC 1,360 ms
131,932 KB
testcase_02 AC 1,204 ms
132,172 KB
testcase_03 AC 1,397 ms
131,384 KB
testcase_04 AC 1,386 ms
132,308 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 1,148 ms
106,636 KB
testcase_09 AC 1,201 ms
121,796 KB
testcase_10 AC 1,729 ms
144,880 KB
testcase_11 TLE -
testcase_12 AC 1,030 ms
107,520 KB
testcase_13 AC 620 ms
70,336 KB
testcase_14 AC 1,723 ms
140,844 KB
testcase_15 AC 1,259 ms
119,300 KB
testcase_16 AC 611 ms
76,392 KB
testcase_17 AC 984 ms
102,840 KB
testcase_18 AC 928 ms
102,312 KB
testcase_19 AC 883 ms
91,728 KB
testcase_20 AC 1,489 ms
131,916 KB
testcase_21 AC 219 ms
58,516 KB
testcase_22 AC 988 ms
104,096 KB
testcase_23 AC 1,122 ms
110,160 KB
testcase_24 AC 860 ms
100,932 KB
testcase_25 AC 1,469 ms
132,164 KB
testcase_26 AC 1,151 ms
111,924 KB
testcase_27 AC 1,280 ms
125,724 KB
testcase_28 AC 1,241 ms
121,468 KB
testcase_29 AC 1,206 ms
126,388 KB
testcase_30 AC 788 ms
84,752 KB
testcase_31 AC 480 ms
66,580 KB
testcase_32 AC 700 ms
85,576 KB
testcase_33 AC 713 ms
84,084 KB
testcase_34 AC 1,141 ms
121,044 KB
testcase_35 AC 242 ms
61,880 KB
testcase_36 AC 1,131 ms
122,412 KB
testcase_37 AC 1,018 ms
105,204 KB
testcase_38 AC 394 ms
68,132 KB
testcase_39 AC 722 ms
87,340 KB
testcase_40 AC 385 ms
61,976 KB
testcase_41 AC 849 ms
89,232 KB
testcase_42 AC 55 ms
53,532 KB
testcase_43 AC 55 ms
53,536 KB
testcase_44 AC 55 ms
53,544 KB
testcase_45 AC 1,140 ms
130,404 KB
testcase_46 AC 1,230 ms
130,360 KB
testcase_47 AC 1,212 ms
130,388 KB
testcase_48 AC 1,222 ms
130,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static long ans = 0;
    static final int MOD = 1000000007;
    static int n;
    static int k;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        n = sc.nextInt();
        int m = sc.nextInt();
        k = 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);
            }
        }
        getCount(0, 0);
        System.out.println(ans);
    }
    
    static long getCount(int idx, int p) {
        long count = 1;
        for (int x : graph.get(idx).keySet()) {
            if (p == x) {
                continue;
            }
            long tmp = getCount(x, idx);
            ans += tmp * (n - tmp) % MOD * pow(k, graph.get(idx).get(x)) % MOD;
            ans %= MOD;
            count += tmp;
        }
        return count;
    }
    
    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 pow(x, p - 1) * x % 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 x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            parents[find(x)] = find(y);
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0