結果
問題 | No.1207 グラフX |
ユーザー | tenten |
提出日時 | 2022-04-08 09:44:52 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,846 ms / 2,000 ms |
コード長 | 2,952 bytes |
コンパイル時間 | 2,928 ms |
コンパイル使用メモリ | 79,472 KB |
実行使用メモリ | 158,136 KB |
最終ジャッジ日時 | 2024-11-28 03:41:58 |
合計ジャッジ時間 | 60,881 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,289 ms
118,840 KB |
testcase_01 | AC | 1,279 ms
118,700 KB |
testcase_02 | AC | 1,236 ms
119,020 KB |
testcase_03 | AC | 1,259 ms
118,916 KB |
testcase_04 | AC | 1,422 ms
119,560 KB |
testcase_05 | AC | 1,846 ms
158,136 KB |
testcase_06 | AC | 1,845 ms
152,852 KB |
testcase_07 | AC | 1,793 ms
157,952 KB |
testcase_08 | AC | 1,146 ms
94,280 KB |
testcase_09 | AC | 1,234 ms
108,232 KB |
testcase_10 | AC | 1,800 ms
143,880 KB |
testcase_11 | AC | 1,811 ms
156,256 KB |
testcase_12 | AC | 1,274 ms
99,384 KB |
testcase_13 | AC | 608 ms
57,304 KB |
testcase_14 | AC | 1,367 ms
117,580 KB |
testcase_15 | AC | 1,224 ms
106,220 KB |
testcase_16 | AC | 654 ms
63,232 KB |
testcase_17 | AC | 1,227 ms
91,992 KB |
testcase_18 | AC | 964 ms
89,236 KB |
testcase_19 | AC | 926 ms
77,228 KB |
testcase_20 | AC | 1,274 ms
118,860 KB |
testcase_21 | AC | 215 ms
44,376 KB |
testcase_22 | AC | 1,071 ms
93,044 KB |
testcase_23 | AC | 1,184 ms
97,152 KB |
testcase_24 | AC | 847 ms
87,608 KB |
testcase_25 | AC | 1,400 ms
119,436 KB |
testcase_26 | AC | 1,225 ms
99,372 KB |
testcase_27 | AC | 1,320 ms
112,540 KB |
testcase_28 | AC | 1,299 ms
110,172 KB |
testcase_29 | AC | 1,430 ms
113,400 KB |
testcase_30 | AC | 710 ms
71,012 KB |
testcase_31 | AC | 518 ms
53,920 KB |
testcase_32 | AC | 708 ms
72,320 KB |
testcase_33 | AC | 752 ms
71,096 KB |
testcase_34 | AC | 1,213 ms
105,304 KB |
testcase_35 | AC | 232 ms
44,704 KB |
testcase_36 | AC | 1,263 ms
109,280 KB |
testcase_37 | AC | 1,059 ms
91,800 KB |
testcase_38 | AC | 451 ms
52,952 KB |
testcase_39 | AC | 766 ms
72,400 KB |
testcase_40 | AC | 367 ms
47,260 KB |
testcase_41 | AC | 890 ms
75,736 KB |
testcase_42 | AC | 53 ms
36,696 KB |
testcase_43 | AC | 54 ms
37,108 KB |
testcase_44 | AC | 54 ms
37,112 KB |
testcase_45 | AC | 1,183 ms
114,804 KB |
testcase_46 | AC | 1,205 ms
114,708 KB |
testcase_47 | AC | 1,179 ms
114,772 KB |
testcase_48 | AC | 1,183 ms
114,576 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { static ArrayList<HashMap<Integer, Integer>> 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 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 int getCount(int idx, int p) { int count = 1; for (int x : graph.get(idx).keySet()) { if (x == p) { continue; } int y = graph.get(idx).get(x); int tmp = getCount(x, idx); ans += (long)tmp * (n - tmp) % MOD * pow(base, y) % 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 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(); } }