結果

問題 No.1207 グラフX
ユーザー 沙耶花沙耶花
提出日時 2020-08-30 13:23:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 299 ms / 2,000 ms
コード長 1,500 bytes
コンパイル時間 1,910 ms
コンパイル使用メモリ 211,748 KB
実行使用メモリ 31,524 KB
最終ジャッジ日時 2024-04-27 06:51:51
合計ジャッジ時間 13,861 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 251 ms
17,444 KB
testcase_01 AC 249 ms
17,276 KB
testcase_02 AC 249 ms
17,316 KB
testcase_03 AC 257 ms
17,316 KB
testcase_04 AC 253 ms
17,320 KB
testcase_05 AC 287 ms
31,400 KB
testcase_06 AC 291 ms
31,400 KB
testcase_07 AC 281 ms
31,396 KB
testcase_08 AC 216 ms
11,476 KB
testcase_09 AC 223 ms
14,816 KB
testcase_10 AC 299 ms
24,196 KB
testcase_11 AC 272 ms
31,524 KB
testcase_12 AC 208 ms
12,380 KB
testcase_13 AC 153 ms
6,940 KB
testcase_14 AC 256 ms
16,712 KB
testcase_15 AC 223 ms
13,524 KB
testcase_16 AC 135 ms
6,940 KB
testcase_17 AC 168 ms
10,980 KB
testcase_18 AC 140 ms
11,184 KB
testcase_19 AC 196 ms
8,024 KB
testcase_20 AC 241 ms
16,968 KB
testcase_21 AC 27 ms
6,940 KB
testcase_22 AC 169 ms
11,176 KB
testcase_23 AC 181 ms
12,384 KB
testcase_24 AC 121 ms
10,236 KB
testcase_25 AC 249 ms
16,788 KB
testcase_26 AC 196 ms
13,160 KB
testcase_27 AC 233 ms
15,404 KB
testcase_28 AC 221 ms
14,852 KB
testcase_29 AC 214 ms
15,328 KB
testcase_30 AC 126 ms
7,932 KB
testcase_31 AC 130 ms
6,940 KB
testcase_32 AC 95 ms
8,440 KB
testcase_33 AC 104 ms
7,988 KB
testcase_34 AC 213 ms
13,344 KB
testcase_35 AC 28 ms
6,940 KB
testcase_36 AC 213 ms
14,648 KB
testcase_37 AC 201 ms
11,516 KB
testcase_38 AC 54 ms
6,940 KB
testcase_39 AC 115 ms
9,196 KB
testcase_40 AC 122 ms
6,944 KB
testcase_41 AC 176 ms
8,460 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 1 ms
6,940 KB
testcase_44 AC 1 ms
6,944 KB
testcase_45 AC 228 ms
17,476 KB
testcase_46 AC 218 ms
17,472 KB
testcase_47 AC 223 ms
17,340 KB
testcase_48 AC 234 ms
17,340 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:60:9: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   60 | int dfs(auto &E,int now,int p){
      |         ^~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000

struct unionfind{
	vector<int> data;
	vector<int> size;
	unionfind(int n){
		for(int i=0;i<n;i++){
			data.push_back(i);
			size.push_back(1);
		}
	}
	
	int find(int x){
		if(data[x]==x)return x;
		return data[x]=find(data[x]);
	}
	
	bool unite(int x,int y){
		x=find(x);y=find(y);
		if(x==y)return false;
		if(size[x]>size[y])swap(x,y);
		data[x]=y;
		size[y]+=size[x];
		return true;
	}
	
	bool check(int x,int y){
		return (find(x)==find(y));
	}
	
	int get_size(int x){
		int X = find(x);
		return size[X];
	}
};
int beki(long long a,long long b,int M = modulo){
	int x = 1;
	while(b!=0){
		if(b&1){
			x=((long long)x*a)%M;
		}
		a=((long long)a*a)%M;
		b>>=1;
	}
	return x;
}


int gyakugen(int a){
	return beki(a,modulo-2);
}

int N,M,X;
int ans = 0;

int dfs(auto &E,int now,int p){
	int ret = 0;
	for(int i=0;i<E[now].size();i++){
		int to = E[now][i].first;
		int z = E[now][i].second;
		if(to==p)continue;
		int t = dfs(E,to,now);
		int Y = mod(t*(N-t));
		ans = mod(ans + mod(beki(X,z)*Y));
		
		ret += t;
	}
	return ret+1;
}

int main(){
	
	cin>>N>>M>>X;
	
	vector<vector<pair<int,int>>> E(N,vector<pair<int,int>>());
	
	unionfind uf(N);
	
	for(int i=0;i<M;i++){
		int x,y,z;
		cin>>x>>y>>z;
		x--;y--;
		if(uf.unite(x,y)){
			E[x].emplace_back(y,z);
			E[y].emplace_back(x,z);
		}
	}
	
	dfs(E,0,-1);
	
	cout<<ans<<endl;
	
	return 0;
}
0