結果
問題 | No.1207 グラフX |
ユーザー | tenten |
提出日時 | 2022-04-08 09:44:52 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,702 ms / 2,000 ms |
コード長 | 2,952 bytes |
コンパイル時間 | 3,137 ms |
コンパイル使用メモリ | 78,812 KB |
実行使用メモリ | 181,564 KB |
最終ジャッジ日時 | 2024-05-06 01:49:35 |
合計ジャッジ時間 | 55,704 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,210 ms
118,624 KB |
testcase_01 | AC | 1,155 ms
118,080 KB |
testcase_02 | AC | 1,118 ms
119,000 KB |
testcase_03 | AC | 1,503 ms
118,664 KB |
testcase_04 | AC | 1,109 ms
119,392 KB |
testcase_05 | AC | 1,690 ms
156,588 KB |
testcase_06 | AC | 1,694 ms
155,024 KB |
testcase_07 | AC | 1,532 ms
181,564 KB |
testcase_08 | AC | 1,003 ms
94,168 KB |
testcase_09 | AC | 1,115 ms
109,648 KB |
testcase_10 | AC | 1,240 ms
145,776 KB |
testcase_11 | AC | 1,702 ms
152,340 KB |
testcase_12 | AC | 1,103 ms
96,132 KB |
testcase_13 | AC | 503 ms
56,944 KB |
testcase_14 | AC | 1,194 ms
117,948 KB |
testcase_15 | AC | 1,081 ms
106,640 KB |
testcase_16 | AC | 580 ms
63,364 KB |
testcase_17 | AC | 841 ms
89,896 KB |
testcase_18 | AC | 950 ms
96,564 KB |
testcase_19 | AC | 829 ms
77,136 KB |
testcase_20 | AC | 1,152 ms
118,432 KB |
testcase_21 | AC | 194 ms
44,124 KB |
testcase_22 | AC | 1,023 ms
90,852 KB |
testcase_23 | AC | 993 ms
96,896 KB |
testcase_24 | AC | 897 ms
87,552 KB |
testcase_25 | AC | 1,252 ms
119,284 KB |
testcase_26 | AC | 1,042 ms
98,860 KB |
testcase_27 | AC | 1,207 ms
112,304 KB |
testcase_28 | AC | 1,276 ms
108,232 KB |
testcase_29 | AC | 1,144 ms
113,456 KB |
testcase_30 | AC | 762 ms
71,024 KB |
testcase_31 | AC | 422 ms
54,044 KB |
testcase_32 | AC | 584 ms
72,272 KB |
testcase_33 | AC | 756 ms
71,144 KB |
testcase_34 | AC | 1,035 ms
105,672 KB |
testcase_35 | AC | 198 ms
44,728 KB |
testcase_36 | AC | 1,059 ms
111,188 KB |
testcase_37 | AC | 1,061 ms
101,100 KB |
testcase_38 | AC | 326 ms
52,744 KB |
testcase_39 | AC | 596 ms
72,464 KB |
testcase_40 | AC | 320 ms
47,692 KB |
testcase_41 | AC | 761 ms
76,176 KB |
testcase_42 | AC | 45 ms
36,984 KB |
testcase_43 | AC | 44 ms
36,776 KB |
testcase_44 | AC | 45 ms
36,892 KB |
testcase_45 | AC | 1,254 ms
114,624 KB |
testcase_46 | AC | 1,040 ms
114,716 KB |
testcase_47 | AC | 1,062 ms
114,664 KB |
testcase_48 | AC | 1,261 ms
114,588 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(); } }