結果

問題 No.1207 グラフX
ユーザー ks2mks2m
提出日時 2020-08-30 14:01:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,637 ms / 2,000 ms
コード長 2,424 bytes
コンパイル時間 3,530 ms
コンパイル使用メモリ 78,328 KB
実行使用メモリ 124,264 KB
最終ジャッジ日時 2024-04-27 07:16:25
合計ジャッジ時間 45,452 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,162 ms
105,128 KB
testcase_01 AC 1,067 ms
97,108 KB
testcase_02 AC 1,135 ms
96,000 KB
testcase_03 AC 1,077 ms
97,288 KB
testcase_04 AC 1,098 ms
96,072 KB
testcase_05 AC 1,253 ms
123,348 KB
testcase_06 AC 1,210 ms
122,180 KB
testcase_07 AC 1,392 ms
116,068 KB
testcase_08 AC 822 ms
82,664 KB
testcase_09 AC 804 ms
87,092 KB
testcase_10 AC 1,637 ms
114,012 KB
testcase_11 AC 1,287 ms
124,264 KB
testcase_12 AC 905 ms
80,680 KB
testcase_13 AC 458 ms
62,964 KB
testcase_14 AC 1,079 ms
99,008 KB
testcase_15 AC 873 ms
87,000 KB
testcase_16 AC 471 ms
62,544 KB
testcase_17 AC 668 ms
78,664 KB
testcase_18 AC 602 ms
76,316 KB
testcase_19 AC 631 ms
71,108 KB
testcase_20 AC 1,258 ms
102,840 KB
testcase_21 AC 195 ms
55,996 KB
testcase_22 AC 688 ms
78,784 KB
testcase_23 AC 744 ms
85,216 KB
testcase_24 AC 589 ms
75,136 KB
testcase_25 AC 1,104 ms
96,888 KB
testcase_26 AC 803 ms
85,548 KB
testcase_27 AC 1,042 ms
101,496 KB
testcase_28 AC 919 ms
88,648 KB
testcase_29 AC 819 ms
94,140 KB
testcase_30 AC 517 ms
67,200 KB
testcase_31 AC 428 ms
60,496 KB
testcase_32 AC 474 ms
69,936 KB
testcase_33 AC 495 ms
69,060 KB
testcase_34 AC 783 ms
87,240 KB
testcase_35 AC 197 ms
56,116 KB
testcase_36 AC 844 ms
87,016 KB
testcase_37 AC 741 ms
79,800 KB
testcase_38 AC 308 ms
62,040 KB
testcase_39 AC 514 ms
72,728 KB
testcase_40 AC 376 ms
58,212 KB
testcase_41 AC 647 ms
74,300 KB
testcase_42 AC 48 ms
49,976 KB
testcase_43 AC 51 ms
49,992 KB
testcase_44 AC 47 ms
50,300 KB
testcase_45 AC 1,179 ms
105,760 KB
testcase_46 AC 1,051 ms
96,020 KB
testcase_47 AC 941 ms
95,828 KB
testcase_48 AC 1,026 ms
97,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
	static List<List<Hen>> list;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		int x = Integer.parseInt(sa[2]);
		list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		UnionFind uf = new UnionFind(n);
		List<Hen> hens = new ArrayList<>();
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			Hen h = new Hen();
			h.x = Integer.parseInt(sa[0]) - 1;
			h.y = Integer.parseInt(sa[1]) - 1;
			h.z = Integer.parseInt(sa[2]);
			if (!uf.same(h.x, h.y)) {
				uf.union(h.x, h.y);
				list.get(h.x).add(h);
				list.get(h.y).add(h);
				hens.add(h);
			}
		}
		br.close();

		dfs(0, -1);
		int mod = 1000000007;
		long ans = 0;
		for (Hen h : hens) {
			long cnt = (long) h.c * (n - h.c) % mod;
			ans += power(x, h.z, mod) * cnt;
			ans %= mod;
		}
		System.out.println(ans);
	}

	static int dfs(int cur, int p) {
		int ret = 1;
		List<Hen> nexts = list.get(cur);
		for (Hen h : nexts) {
			int next = h.x;
			if (next == cur) {
				next = h.y;
			}
			if (next == p) {
				continue;
			}
			h.c = dfs(next, cur);
			ret += h.c;
		}
		return ret;
	}

	static class Hen {
		int x, y, z, c;
	}

	static long power(long x, long n, int m) {
		if (n == 0) {
			return 1;
		}
		long val = power(x, n / 2, m);
		val = val * val % m;
		if (n % 2 == 1) {
			val = val * x % m;
		}
		return val;
	}

	static class UnionFind {
		int[] parent, size;
		int num = 0; // 連結成分の数

		UnionFind(int n) {
			parent = new int[n];
			size = new int[n];
			num = n;
			for (int i = 0; i < n; i++) {
				parent[i] = i;
				size[i] = 1;
			}
		}

		void union(int x, int y) {
			int px = find(x);
			int py = find(y);
			if (px != py) {
				parent[px] = py;
				size[py] += size[px];
				num--;
			}
		}

		int find(int x) {
			if (parent[x] == x) {
				return x;
			}
			parent[x] = find(parent[x]);
			return parent[x];
		}

		/**
		 * xとyが同一連結成分か
		 */
		boolean same(int x, int y) {
			return find(x) == find(y);
		}

		/**
		 * xを含む連結成分のサイズ
		 */
		int size(int x) {
			return size[find(x)];
		}
	}
}
0