結果

問題 No.1207 グラフX
ユーザー leaf_1415leaf_1415
提出日時 2020-08-30 13:14:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 293 ms / 2,000 ms
コード長 2,243 bytes
コンパイル時間 2,426 ms
コンパイル使用メモリ 88,920 KB
実行使用メモリ 39,652 KB
最終ジャッジ日時 2023-08-09 11:39:24
合計ジャッジ時間 12,297 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 218 ms
23,864 KB
testcase_01 AC 222 ms
23,268 KB
testcase_02 AC 221 ms
22,656 KB
testcase_03 AC 230 ms
23,308 KB
testcase_04 AC 226 ms
23,328 KB
testcase_05 AC 292 ms
39,652 KB
testcase_06 AC 282 ms
39,244 KB
testcase_07 AC 286 ms
39,492 KB
testcase_08 AC 174 ms
19,344 KB
testcase_09 AC 187 ms
23,068 KB
testcase_10 AC 283 ms
32,280 KB
testcase_11 AC 293 ms
39,328 KB
testcase_12 AC 173 ms
20,228 KB
testcase_13 AC 88 ms
15,580 KB
testcase_14 AC 212 ms
22,608 KB
testcase_15 AC 184 ms
20,428 KB
testcase_16 AC 88 ms
15,560 KB
testcase_17 AC 143 ms
17,980 KB
testcase_18 AC 123 ms
16,608 KB
testcase_19 AC 132 ms
16,860 KB
testcase_20 AC 218 ms
22,744 KB
testcase_21 AC 17 ms
10,184 KB
testcase_22 AC 137 ms
18,268 KB
testcase_23 AC 149 ms
18,788 KB
testcase_24 AC 103 ms
15,784 KB
testcase_25 AC 221 ms
22,508 KB
testcase_26 AC 167 ms
19,128 KB
testcase_27 AC 200 ms
22,208 KB
testcase_28 AC 190 ms
22,760 KB
testcase_29 AC 194 ms
22,012 KB
testcase_30 AC 96 ms
14,532 KB
testcase_31 AC 71 ms
16,300 KB
testcase_32 AC 85 ms
14,040 KB
testcase_33 AC 89 ms
13,972 KB
testcase_34 AC 180 ms
19,760 KB
testcase_35 AC 19 ms
10,232 KB
testcase_36 AC 179 ms
21,952 KB
testcase_37 AC 157 ms
18,188 KB
testcase_38 AC 43 ms
11,164 KB
testcase_39 AC 95 ms
15,288 KB
testcase_40 AC 55 ms
16,016 KB
testcase_41 AC 124 ms
17,600 KB
testcase_42 AC 5 ms
8,320 KB
testcase_43 AC 5 ms
8,364 KB
testcase_44 AC 5 ms
8,416 KB
testcase_45 AC 210 ms
23,068 KB
testcase_46 AC 209 ms
23,032 KB
testcase_47 AC 214 ms
23,264 KB
testcase_48 AC 213 ms
23,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;
typedef pair<llint, P> E;

struct UnionFind{
	int size;
	vector<int> parent;
	
	UnionFind(){}
	UnionFind(int size){
		this->size = size;
		parent.resize(size+1);
		init();
	}
	void init(){
		for(int i = 0; i <= size; i++) parent[i] = i;
	}
	int root(int i){
		if(parent[i] == i) return i;
		return parent[i] = root(parent[i]);
	}
	bool same(int i, int j){
		return root(i) == root(j);
	}
	void unite(int i, int j){
		int root_i = root(i), root_j = root(j);
		if(root_i == root_j) return;
		parent[root_i] = root_j;
	}
};

struct edge{
	llint to, cost;
	edge(){}
	edge(llint a, llint b){
		to = a, cost = b;
	}
};

llint modpow(llint a, llint n)
{
	if(n == 0) return 1;
	if(n % 2){
		return ((a%mod) * (modpow(a, n-1)%mod)) % mod;
	}
	else{
		return modpow((a*a)%mod, n/2) % mod;
	}
}

llint n, m, x;
vector<edge> G[200005];
vector<E> vec;
UnionFind uf(200005);

llint ans = 0;
llint dfs(int v, int p)
{
	llint ret = 1;
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i].to == p) continue;
		llint res = dfs(G[v][i].to, v);
		ans += res * (n-res) % mod * modpow(x, G[v][i].cost) % mod;
		ans %= mod;
		ret += res;
	}
	return ret;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m >> x;
	llint u, v, w;
	for(int i = 1; i <= m; i++){
		cin >> u >> v >> w;
		vec.push_back(E(w, P(u, v)));
	}
	sort(vec.begin(), vec.end());
	
	for(int i = 0; i < vec.size(); i++){
		llint w = vec[i].first, u = vec[i].second.first, v = vec[i].second.second;
		if(uf.same(u, v)) continue;
		uf.unite(u, v);
		G[u].push_back(edge(v, w));
		G[v].push_back(edge(u, w));
	}
	
	dfs(1, -1);
	cout << ans << endl;
	
	return 0;
}
0