結果

問題 No.1207 グラフX
ユーザー yuma220284yuma220284
提出日時 2020-08-30 13:31:38
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,666 bytes
コンパイル時間 3,832 ms
コンパイル使用メモリ 156,456 KB
実行使用メモリ 28,292 KB
最終ジャッジ日時 2023-08-09 11:50:36
合計ジャッジ時間 19,862 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 175 ms
5,716 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 170 ms
7,004 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 245 ms
9,212 KB
testcase_20 WA -
testcase_21 AC 30 ms
4,380 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 162 ms
8,644 KB
testcase_31 AC 149 ms
4,980 KB
testcase_32 AC 135 ms
9,328 KB
testcase_33 AC 144 ms
8,904 KB
testcase_34 WA -
testcase_35 AC 31 ms
4,376 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 68 ms
5,792 KB
testcase_39 AC 152 ms
9,716 KB
testcase_40 AC 122 ms
4,376 KB
testcase_41 AC 223 ms
9,320 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 319 ms
20,348 KB
testcase_46 AC 322 ms
20,428 KB
testcase_47 AC 321 ms
20,352 KB
testcase_48 AC 323 ms
20,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

class UnionFind {
private:
	vector<int> Rank, Vol, Root;
public:
	UnionFind(int N) : Rank(N, 1), Vol(N, 1), Root(N) {
		for (int i = 0; i < N; i++) Root[i] = i;
	}
	int Find(int N) { return (N == Root[N] ? N : Root[N] = Find(Root[N])); }
	bool Same(int X, int Y) { return Find(X) == Find(Y); }
	void Union(int X, int Y) {
		X = Find(X), Y = Find(Y);
		if (X == Y) return;
		if (Rank[X] > Rank[Y]) Root[X] = Y, Vol[Y] += Vol[X];
		else if (Rank[X] < Rank[Y]) Root[Y] = X, Vol[X] += Vol[Y];
		else Root[Y] = X, Vol[X] += Vol[Y], Rank[X]++;
	}
	int Size(int N) { return Vol[Find(N)]; }
};

constexpr long long MOD = 1000000007;

long long Pow(long long A, long long B) {
	if (B == 0) return 1;
	if (B % 2 == 0) {
		long long C = Pow(A, B / 2);
		return (C * C) % MOD;
	}
	return (A * Pow(A, B - 1)) % MOD;
}

vector<pair<int, int> > Par;
vector<vector<pair<int, int> > > V;
vector<int> Size;

void DFS(int P) {
	Size[P] = 1;
	for (pair<int, int> NP : V[P]) {
		if (Par[NP.first].first == -2) {
			Par[NP.first] = { P, NP.second };
			DFS(NP.first);
			Size[P] += Size[NP.first];
		}
	}
}

int main() {
	int N, M;
	long long X;
	cin >> N >> M >> X;
	UnionFind UF(N);
	V.resize(N);
	for (int i = 0; i < M; i++) {
		int S, T;
		long long Z;
		cin >> S >> T >> Z;
		S--, T--;
		if (!UF.Same(S, T)) {
			V[S].push_back({ T, Z });
			V[T].push_back({ S, Z });
			UF.Union(S, T);
		}
	}
	Size.resize(N);
	Par.assign(N, { -2, -1 });
	Par[0] = { -1, 0 };
	DFS(0);
	long long ANS = 0;
	for (int i = 1; i < N; i++) {
		ANS += (Pow(X, Par[i].second) * ((Size[i] * (N - Size[i])) % MOD)) % MOD;
		ANS %= MOD;
	}
	cout << ANS << endl;
}
0