結果

問題 No.1207 グラフX
ユーザー 👑 kmjpkmjp
提出日時 2020-08-30 14:59:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 1,881 bytes
コンパイル時間 2,238 ms
コンパイル使用メモリ 207,828 KB
実行使用メモリ 29,720 KB
最終ジャッジ日時 2023-08-09 12:51:25
合計ジャッジ時間 11,488 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 179 ms
19,064 KB
testcase_01 AC 177 ms
18,900 KB
testcase_02 AC 179 ms
19,364 KB
testcase_03 AC 181 ms
18,808 KB
testcase_04 AC 178 ms
19,676 KB
testcase_05 AC 230 ms
29,584 KB
testcase_06 AC 234 ms
29,604 KB
testcase_07 AC 236 ms
29,612 KB
testcase_08 AC 150 ms
15,060 KB
testcase_09 AC 162 ms
16,464 KB
testcase_10 AC 220 ms
24,064 KB
testcase_11 AC 222 ms
29,720 KB
testcase_12 AC 139 ms
15,508 KB
testcase_13 AC 70 ms
11,368 KB
testcase_14 AC 174 ms
18,176 KB
testcase_15 AC 155 ms
16,468 KB
testcase_16 AC 72 ms
11,928 KB
testcase_17 AC 112 ms
14,592 KB
testcase_18 AC 86 ms
14,904 KB
testcase_19 AC 106 ms
13,080 KB
testcase_20 AC 175 ms
18,564 KB
testcase_21 AC 15 ms
10,108 KB
testcase_22 AC 118 ms
14,756 KB
testcase_23 AC 127 ms
15,600 KB
testcase_24 AC 80 ms
14,460 KB
testcase_25 AC 170 ms
18,268 KB
testcase_26 AC 130 ms
15,992 KB
testcase_27 AC 173 ms
17,456 KB
testcase_28 AC 156 ms
16,576 KB
testcase_29 AC 152 ms
17,500 KB
testcase_30 AC 77 ms
12,884 KB
testcase_31 AC 59 ms
10,880 KB
testcase_32 AC 63 ms
13,016 KB
testcase_33 AC 69 ms
13,060 KB
testcase_34 AC 147 ms
16,288 KB
testcase_35 AC 16 ms
10,316 KB
testcase_36 AC 151 ms
16,616 KB
testcase_37 AC 131 ms
15,196 KB
testcase_38 AC 34 ms
11,608 KB
testcase_39 AC 71 ms
13,664 KB
testcase_40 AC 44 ms
10,312 KB
testcase_41 AC 100 ms
13,188 KB
testcase_42 AC 6 ms
10,020 KB
testcase_43 AC 6 ms
9,968 KB
testcase_44 AC 6 ms
10,016 KB
testcase_45 AC 168 ms
18,896 KB
testcase_46 AC 176 ms
18,736 KB
testcase_47 AC 164 ms
18,776 KB
testcase_48 AC 164 ms
18,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

const ll mo=1000000007;
ll modpow(ll a, ll n = mo-2) {
	ll r=1;a%=mo;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

template<int um> class UF {
	public:
	vector<int> par,rank,cnt;
	UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
	void reinit() {int i; FOR(i,um) rank[i]=0,cnt[i]=1,par[i]=i;}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int count(int x) { return cnt[operator[](x)];}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		cnt[y]=cnt[x]=cnt[x]+cnt[y];
		if(rank[x]>rank[y]) return par[x]=y;
		rank[x]+=rank[x]==rank[y]; return par[y]=x;
	}
};
UF<202020> uf;

int N,M,X;
vector<pair<int,int>> E[202020];

int C[202020];

ll ret;
int dfs(int cur,int pre) {
	C[cur]=1;
	FORR(e,E[cur]) if(e.first!=pre) {
		C[cur]+=dfs(e.first,cur);
	}
	FORR(e,E[cur]) if(e.first==pre) {
		ret+=1LL*C[cur]*(N-C[cur])%mo*modpow(X,e.second)%mo;
	}
	return C[cur];
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M>>X;
	FOR(i,M) {
		cin>>x>>y>>r;
		x--,y--;
		if(uf[x]!=uf[y]) {
			E[x].push_back({y,r});
			E[y].push_back({x,r});
			uf(x,y);
		}
	}
	dfs(0,0);
	cout<<ret%mo<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0