結果

問題 No.1207 グラフX
ユーザー 👑 kmjpkmjp
提出日時 2020-08-30 14:59:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 1,881 bytes
コンパイル時間 2,270 ms
コンパイル使用メモリ 211,824 KB
実行使用メモリ 29,844 KB
最終ジャッジ日時 2024-04-27 07:53:10
合計ジャッジ時間 10,013 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
19,228 KB
testcase_01 AC 132 ms
19,012 KB
testcase_02 AC 128 ms
19,528 KB
testcase_03 AC 132 ms
19,012 KB
testcase_04 AC 130 ms
19,716 KB
testcase_05 AC 178 ms
29,820 KB
testcase_06 AC 166 ms
29,844 KB
testcase_07 AC 174 ms
29,828 KB
testcase_08 AC 119 ms
15,260 KB
testcase_09 AC 121 ms
16,684 KB
testcase_10 AC 165 ms
24,080 KB
testcase_11 AC 157 ms
29,812 KB
testcase_12 AC 111 ms
15,888 KB
testcase_13 AC 63 ms
11,924 KB
testcase_14 AC 133 ms
18,532 KB
testcase_15 AC 115 ms
16,588 KB
testcase_16 AC 65 ms
12,136 KB
testcase_17 AC 96 ms
15,004 KB
testcase_18 AC 76 ms
15,236 KB
testcase_19 AC 90 ms
13,456 KB
testcase_20 AC 130 ms
18,836 KB
testcase_21 AC 14 ms
10,380 KB
testcase_22 AC 90 ms
15,272 KB
testcase_23 AC 100 ms
16,212 KB
testcase_24 AC 69 ms
14,708 KB
testcase_25 AC 131 ms
18,700 KB
testcase_26 AC 104 ms
16,420 KB
testcase_27 AC 126 ms
17,752 KB
testcase_28 AC 122 ms
16,960 KB
testcase_29 AC 126 ms
17,612 KB
testcase_30 AC 66 ms
13,080 KB
testcase_31 AC 53 ms
11,056 KB
testcase_32 AC 54 ms
13,440 KB
testcase_33 AC 59 ms
13,204 KB
testcase_34 AC 110 ms
16,488 KB
testcase_35 AC 14 ms
10,496 KB
testcase_36 AC 111 ms
16,772 KB
testcase_37 AC 101 ms
15,312 KB
testcase_38 AC 29 ms
12,176 KB
testcase_39 AC 57 ms
13,604 KB
testcase_40 AC 40 ms
10,576 KB
testcase_41 AC 84 ms
13,240 KB
testcase_42 AC 6 ms
10,216 KB
testcase_43 AC 5 ms
10,360 KB
testcase_44 AC 4 ms
10,512 KB
testcase_45 AC 122 ms
18,976 KB
testcase_46 AC 132 ms
19,000 KB
testcase_47 AC 120 ms
19,000 KB
testcase_48 AC 122 ms
19,000 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