結果

問題 No.1207 グラフX
ユーザー ks2mks2m
提出日時 2020-08-30 14:01:52
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,424 bytes
コンパイル時間 5,116 ms
コンパイル使用メモリ 74,740 KB
実行使用メモリ 113,408 KB
最終ジャッジ日時 2023-08-09 12:11:25
合計ジャッジ時間 56,505 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,206 ms
98,796 KB
testcase_01 AC 1,096 ms
98,348 KB
testcase_02 AC 1,095 ms
98,272 KB
testcase_03 AC 1,105 ms
98,768 KB
testcase_04 AC 1,110 ms
98,236 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 908 ms
79,936 KB
testcase_09 AC 1,020 ms
88,656 KB
testcase_10 AC 1,913 ms
105,952 KB
testcase_11 TLE -
testcase_12 AC 906 ms
86,108 KB
testcase_13 AC 538 ms
63,012 KB
testcase_14 AC 1,112 ms
97,868 KB
testcase_15 AC 1,066 ms
87,636 KB
testcase_16 AC 530 ms
63,288 KB
testcase_17 AC 785 ms
79,224 KB
testcase_18 AC 789 ms
78,440 KB
testcase_19 AC 725 ms
71,784 KB
testcase_20 AC 1,229 ms
98,436 KB
testcase_21 AC 208 ms
58,436 KB
testcase_22 AC 826 ms
79,296 KB
testcase_23 AC 850 ms
84,280 KB
testcase_24 AC 633 ms
77,100 KB
testcase_25 AC 1,181 ms
100,596 KB
testcase_26 AC 970 ms
88,892 KB
testcase_27 AC 1,154 ms
98,988 KB
testcase_28 AC 1,045 ms
89,372 KB
testcase_29 AC 1,100 ms
95,712 KB
testcase_30 AC 612 ms
66,724 KB
testcase_31 AC 486 ms
60,064 KB
testcase_32 AC 567 ms
71,120 KB
testcase_33 AC 580 ms
69,984 KB
testcase_34 AC 1,040 ms
88,036 KB
testcase_35 AC 216 ms
56,264 KB
testcase_36 AC 1,014 ms
90,728 KB
testcase_37 AC 854 ms
79,120 KB
testcase_38 AC 360 ms
61,948 KB
testcase_39 AC 591 ms
70,784 KB
testcase_40 AC 414 ms
57,944 KB
testcase_41 AC 714 ms
74,360 KB
testcase_42 AC 44 ms
49,456 KB
testcase_43 AC 44 ms
49,316 KB
testcase_44 AC 44 ms
49,516 KB
testcase_45 AC 1,057 ms
97,956 KB
testcase_46 AC 1,042 ms
98,128 KB
testcase_47 AC 1,093 ms
99,452 KB
testcase_48 AC 1,030 ms
96,652 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