結果

問題 No.1207 グラフX
ユーザー yuma220284yuma220284
提出日時 2020-08-30 13:31:38
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,666 bytes
コンパイル時間 2,015 ms
コンパイル使用メモリ 170,916 KB
実行使用メモリ 34,624 KB
最終ジャッジ日時 2024-04-27 06:57:14
合計ジャッジ時間 17,366 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 179 ms
6,940 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 170 ms
6,944 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 251 ms
9,216 KB
testcase_20 WA -
testcase_21 AC 31 ms
6,940 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 161 ms
8,832 KB
testcase_31 AC 159 ms
6,940 KB
testcase_32 AC 135 ms
9,216 KB
testcase_33 AC 142 ms
9,088 KB
testcase_34 WA -
testcase_35 AC 32 ms
6,944 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 68 ms
6,940 KB
testcase_39 AC 148 ms
9,800 KB
testcase_40 AC 128 ms
6,940 KB
testcase_41 AC 224 ms
9,216 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 1 ms
6,944 KB
testcase_45 AC 313 ms
20,432 KB
testcase_46 AC 326 ms
20,304 KB
testcase_47 AC 318 ms
20,304 KB
testcase_48 AC 317 ms
20,432 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