結果
問題 | No.1207 グラフX |
ユーザー | tenten |
提出日時 | 2023-03-17 17:49:01 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,481 bytes |
コンパイル時間 | 2,788 ms |
コンパイル使用メモリ | 90,704 KB |
実行使用メモリ | 142,168 KB |
最終ジャッジ日時 | 2024-09-18 10:05:30 |
合計ジャッジ時間 | 61,618 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,273 ms
118,468 KB |
testcase_01 | AC | 1,323 ms
118,656 KB |
testcase_02 | AC | 1,229 ms
119,108 KB |
testcase_03 | AC | 1,208 ms
119,720 KB |
testcase_04 | AC | 1,410 ms
118,920 KB |
testcase_05 | TLE | - |
testcase_06 | TLE | - |
testcase_07 | TLE | - |
testcase_08 | AC | 1,141 ms
93,236 KB |
testcase_09 | AC | 1,220 ms
108,408 KB |
testcase_10 | AC | 1,364 ms
133,728 KB |
testcase_11 | TLE | - |
testcase_12 | AC | 1,089 ms
94,284 KB |
testcase_13 | AC | 628 ms
57,544 KB |
testcase_14 | AC | 1,288 ms
117,688 KB |
testcase_15 | AC | 1,277 ms
106,316 KB |
testcase_16 | AC | 725 ms
68,576 KB |
testcase_17 | AC | 1,026 ms
89,044 KB |
testcase_18 | AC | 939 ms
89,180 KB |
testcase_19 | AC | 903 ms
76,652 KB |
testcase_20 | AC | 1,264 ms
118,856 KB |
testcase_21 | AC | 220 ms
44,540 KB |
testcase_22 | AC | 1,002 ms
90,700 KB |
testcase_23 | AC | 1,169 ms
97,212 KB |
testcase_24 | AC | 966 ms
94,948 KB |
testcase_25 | AC | 1,358 ms
118,780 KB |
testcase_26 | AC | 1,148 ms
98,560 KB |
testcase_27 | AC | 1,291 ms
112,568 KB |
testcase_28 | AC | 1,237 ms
107,964 KB |
testcase_29 | AC | 1,302 ms
113,068 KB |
testcase_30 | AC | 731 ms
76,676 KB |
testcase_31 | AC | 485 ms
53,740 KB |
testcase_32 | AC | 716 ms
71,892 KB |
testcase_33 | AC | 724 ms
71,212 KB |
testcase_34 | AC | 1,178 ms
104,776 KB |
testcase_35 | AC | 232 ms
45,596 KB |
testcase_36 | AC | 1,136 ms
108,248 KB |
testcase_37 | AC | 1,062 ms
91,644 KB |
testcase_38 | AC | 407 ms
55,448 KB |
testcase_39 | AC | 768 ms
75,928 KB |
testcase_40 | AC | 382 ms
48,308 KB |
testcase_41 | AC | 860 ms
75,400 KB |
testcase_42 | AC | 52 ms
37,108 KB |
testcase_43 | AC | 52 ms
37,024 KB |
testcase_44 | AC | 52 ms
36,612 KB |
testcase_45 | AC | 1,147 ms
114,612 KB |
testcase_46 | AC | 1,191 ms
115,044 KB |
testcase_47 | AC | 1,145 ms
114,900 KB |
testcase_48 | AC | 1,172 ms
115,008 KB |
ソースコード
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(); } }