結果
問題 | No.1207 グラフX |
ユーザー | tenten |
提出日時 | 2021-12-01 10:19:20 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,642 ms / 2,000 ms |
コード長 | 3,201 bytes |
コンパイル時間 | 2,631 ms |
コンパイル使用メモリ | 79,136 KB |
実行使用メモリ | 132,444 KB |
最終ジャッジ日時 | 2024-07-04 09:31:49 |
合計ジャッジ時間 | 51,304 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,048 ms
95,700 KB |
testcase_01 | AC | 1,219 ms
100,848 KB |
testcase_02 | AC | 964 ms
85,876 KB |
testcase_03 | AC | 919 ms
87,248 KB |
testcase_04 | AC | 976 ms
96,488 KB |
testcase_05 | AC | 1,641 ms
124,180 KB |
testcase_06 | AC | 1,342 ms
132,444 KB |
testcase_07 | AC | 1,303 ms
120,080 KB |
testcase_08 | AC | 875 ms
70,756 KB |
testcase_09 | AC | 889 ms
76,460 KB |
testcase_10 | AC | 1,576 ms
91,668 KB |
testcase_11 | AC | 1,642 ms
105,908 KB |
testcase_12 | AC | 949 ms
74,936 KB |
testcase_13 | AC | 503 ms
53,128 KB |
testcase_14 | AC | 899 ms
85,148 KB |
testcase_15 | AC | 903 ms
78,100 KB |
testcase_16 | AC | 544 ms
54,832 KB |
testcase_17 | AC | 857 ms
68,112 KB |
testcase_18 | AC | 624 ms
70,392 KB |
testcase_19 | AC | 629 ms
60,304 KB |
testcase_20 | AC | 891 ms
85,856 KB |
testcase_21 | AC | 211 ms
44,100 KB |
testcase_22 | AC | 746 ms
77,428 KB |
testcase_23 | AC | 802 ms
74,360 KB |
testcase_24 | AC | 610 ms
64,820 KB |
testcase_25 | AC | 900 ms
85,480 KB |
testcase_26 | AC | 923 ms
85,152 KB |
testcase_27 | AC | 897 ms
95,648 KB |
testcase_28 | AC | 928 ms
78,304 KB |
testcase_29 | AC | 841 ms
78,192 KB |
testcase_30 | AC | 611 ms
59,408 KB |
testcase_31 | AC | 443 ms
51,648 KB |
testcase_32 | AC | 526 ms
58,148 KB |
testcase_33 | AC | 550 ms
58,800 KB |
testcase_34 | AC | 931 ms
77,504 KB |
testcase_35 | AC | 212 ms
44,536 KB |
testcase_36 | AC | 839 ms
88,232 KB |
testcase_37 | AC | 802 ms
67,864 KB |
testcase_38 | AC | 337 ms
48,656 KB |
testcase_39 | AC | 551 ms
60,556 KB |
testcase_40 | AC | 343 ms
47,100 KB |
testcase_41 | AC | 697 ms
71,524 KB |
testcase_42 | AC | 50 ms
37,200 KB |
testcase_43 | AC | 51 ms
37,192 KB |
testcase_44 | AC | 52 ms
37,152 KB |
testcase_45 | AC | 939 ms
85,092 KB |
testcase_46 | AC | 923 ms
98,008 KB |
testcase_47 | AC | 1,158 ms
96,424 KB |
testcase_48 | AC | 874 ms
84,744 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { static int n; static ArrayList<ArrayList<Path>> graph = new ArrayList<>(); static int x; static long ans = 0; static final int MOD = 1000000007; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); n = sc.nextInt(); int m = sc.nextInt(); x = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } UnionFindTree uft = new UnionFindTree(n); for (int i = 0; i < m; i++) { int left = sc.nextInt() - 1; int right = sc.nextInt() - 1; int cost = sc.nextInt(); if (uft.same(left, right)) { continue; } uft.unite(left, right); graph.get(left).add(new Path(right, cost)); graph.get(right).add(new Path(left, cost)); } getCount(0, 0); System.out.println(ans); } static long getCount(int idx, int p) { long count = 1; for (Path y : graph.get(idx)) { if (y.idx == p) { continue; } long tmp = getCount(y.idx, idx); ans += (n - tmp) * tmp % MOD * pow(x, y.value) % 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 Path { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } } static class UnionFindTree { int[] parent; public UnionFindTree(int size) { parent = new int[size]; for (int i = 0; i < size; i++) { parent[i] = i; } } public int find(int x) { if (parent[x] == x) { return x; } else { return parent[x] = find(parent[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public void unite(int x, int y) { if (!same(x, y)) { parent[find(x)] = find(y); } } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); 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 String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }