結果
問題 | No.1207 グラフX |
ユーザー | tenten |
提出日時 | 2022-04-08 13:47:30 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,971 ms / 2,000 ms |
コード長 | 3,119 bytes |
コンパイル時間 | 2,564 ms |
コンパイル使用メモリ | 78,316 KB |
実行使用メモリ | 135,396 KB |
最終ジャッジ日時 | 2024-05-06 02:23:01 |
合計ジャッジ時間 | 47,947 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,100 ms
85,888 KB |
testcase_01 | AC | 946 ms
85,428 KB |
testcase_02 | AC | 1,186 ms
95,728 KB |
testcase_03 | AC | 1,021 ms
86,692 KB |
testcase_04 | AC | 895 ms
85,140 KB |
testcase_05 | AC | 1,383 ms
135,396 KB |
testcase_06 | AC | 1,971 ms
111,256 KB |
testcase_07 | AC | 1,468 ms
105,524 KB |
testcase_08 | AC | 890 ms
68,000 KB |
testcase_09 | AC | 939 ms
75,624 KB |
testcase_10 | AC | 1,123 ms
99,944 KB |
testcase_11 | AC | 1,865 ms
103,648 KB |
testcase_12 | AC | 968 ms
74,724 KB |
testcase_13 | AC | 473 ms
51,132 KB |
testcase_14 | AC | 866 ms
85,288 KB |
testcase_15 | AC | 840 ms
77,876 KB |
testcase_16 | AC | 476 ms
54,160 KB |
testcase_17 | AC | 679 ms
66,964 KB |
testcase_18 | AC | 634 ms
67,380 KB |
testcase_19 | AC | 595 ms
63,340 KB |
testcase_20 | AC | 1,168 ms
99,532 KB |
testcase_21 | AC | 188 ms
43,764 KB |
testcase_22 | AC | 721 ms
67,712 KB |
testcase_23 | AC | 735 ms
73,792 KB |
testcase_24 | AC | 590 ms
67,532 KB |
testcase_25 | AC | 887 ms
85,472 KB |
testcase_26 | AC | 859 ms
75,076 KB |
testcase_27 | AC | 756 ms
82,696 KB |
testcase_28 | AC | 807 ms
75,000 KB |
testcase_29 | AC | 911 ms
77,764 KB |
testcase_30 | AC | 605 ms
59,708 KB |
testcase_31 | AC | 377 ms
51,732 KB |
testcase_32 | AC | 453 ms
57,756 KB |
testcase_33 | AC | 516 ms
58,964 KB |
testcase_34 | AC | 1,031 ms
73,864 KB |
testcase_35 | AC | 204 ms
44,208 KB |
testcase_36 | AC | 755 ms
76,012 KB |
testcase_37 | AC | 646 ms
67,788 KB |
testcase_38 | AC | 287 ms
48,360 KB |
testcase_39 | AC | 474 ms
60,112 KB |
testcase_40 | AC | 329 ms
46,592 KB |
testcase_41 | AC | 587 ms
63,964 KB |
testcase_42 | AC | 45 ms
37,092 KB |
testcase_43 | AC | 45 ms
36,904 KB |
testcase_44 | AC | 45 ms
36,884 KB |
testcase_45 | AC | 1,190 ms
99,824 KB |
testcase_46 | AC | 1,180 ms
96,164 KB |
testcase_47 | AC | 919 ms
87,616 KB |
testcase_48 | AC | 778 ms
84,264 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { static ArrayList<ArrayList<Path>> graph = new ArrayList<>(); static final int MOD = 1000000007; static int n; static int base; static long ans = 0; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); n = sc.nextInt(); int m = sc.nextInt(); base = 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 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).add(new Path(b, c)); graph.get(b).add(new Path(a, c)); } } getCount(0, 0); System.out.println(ans); } static int getCount(int idx, int p) { int count = 1; for (Path x : graph.get(idx)) { if (x.idx == p) { continue; } int tmp = getCount(x.idx, idx); ans += (long)tmp * (n - tmp) % MOD * pow(base, x.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 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); } } static class Path { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } } } 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(); } }