結果

問題 No.1207 グラフX
ユーザー chocoruskchocorusk
提出日時 2020-08-30 21:41:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 325 ms / 2,000 ms
コード長 1,895 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 140,164 KB
実行使用メモリ 28,752 KB
最終ジャッジ日時 2024-04-27 13:23:01
合計ジャッジ時間 14,937 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 274 ms
18,024 KB
testcase_01 AC 280 ms
18,252 KB
testcase_02 AC 277 ms
18,144 KB
testcase_03 AC 274 ms
18,220 KB
testcase_04 AC 292 ms
18,028 KB
testcase_05 AC 320 ms
28,716 KB
testcase_06 AC 319 ms
28,752 KB
testcase_07 AC 320 ms
28,752 KB
testcase_08 AC 243 ms
13,752 KB
testcase_09 AC 252 ms
15,460 KB
testcase_10 AC 307 ms
23,280 KB
testcase_11 AC 325 ms
28,644 KB
testcase_12 AC 237 ms
14,468 KB
testcase_13 AC 166 ms
9,768 KB
testcase_14 AC 262 ms
17,556 KB
testcase_15 AC 252 ms
15,292 KB
testcase_16 AC 152 ms
10,100 KB
testcase_17 AC 196 ms
13,640 KB
testcase_18 AC 149 ms
13,940 KB
testcase_19 AC 218 ms
11,644 KB
testcase_20 AC 290 ms
17,876 KB
testcase_21 AC 34 ms
8,524 KB
testcase_22 AC 191 ms
13,600 KB
testcase_23 AC 209 ms
14,544 KB
testcase_24 AC 138 ms
13,432 KB
testcase_25 AC 288 ms
17,836 KB
testcase_26 AC 222 ms
15,216 KB
testcase_27 AC 259 ms
16,648 KB
testcase_28 AC 254 ms
15,608 KB
testcase_29 AC 250 ms
16,520 KB
testcase_30 AC 141 ms
11,224 KB
testcase_31 AC 151 ms
9,424 KB
testcase_32 AC 109 ms
11,660 KB
testcase_33 AC 122 ms
11,540 KB
testcase_34 AC 245 ms
15,196 KB
testcase_35 AC 32 ms
8,272 KB
testcase_36 AC 231 ms
15,764 KB
testcase_37 AC 207 ms
14,192 KB
testcase_38 AC 57 ms
9,796 KB
testcase_39 AC 121 ms
11,756 KB
testcase_40 AC 128 ms
8,528 KB
testcase_41 AC 192 ms
11,672 KB
testcase_42 AC 3 ms
8,140 KB
testcase_43 AC 3 ms
8,328 KB
testcase_44 AC 4 ms
8,132 KB
testcase_45 AC 259 ms
18,028 KB
testcase_46 AC 248 ms
18,240 KB
testcase_47 AC 246 ms
18,108 KB
testcase_48 AC 248 ms
18,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct unionfind{
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
const ll MOD=1e9+7;
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k>>=1;
    }
    return ans;
}
int main()
{
	int n, m; ll X;
	cin>>n>>m>>X;
	vector<P> g[200020];
	unionfind uf(n);
	for(int i=0; i<m; i++){
		int a, b, c;
		cin>>a>>b>>c;
		a--; b--;
		if(uf.same(a, b)) continue;
		uf.unite(a, b);
		g[a].push_back({b, c});
		g[b].push_back({a, c});
	}
	ll ans=0;
	int cnt[200020];
	auto dfs=[&](auto dfs, int x, int p)->void{
		cnt[x]=1;
		for(auto q:g[x]){
			int y=q.first;
			if(y==p) continue;
			dfs(dfs, y, x);
			(ans+=(ll)cnt[y]*(n-cnt[y])%MOD*powmod(X, q.second))%=MOD;
			cnt[x]+=cnt[y];
		}
	};
	dfs(dfs, 0, -1);
	cout<<ans<<endl;
	return 0;
}
0