結果

問題 No.1207 グラフX
ユーザー penguinmanpenguinman
提出日時 2020-07-30 03:47:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 450 ms / 2,000 ms
コード長 2,182 bytes
コンパイル時間 1,789 ms
コンパイル使用メモリ 179,252 KB
実行使用メモリ 51,532 KB
最終ジャッジ日時 2023-09-18 05:26:26
合計ジャッジ時間 17,747 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 366 ms
28,180 KB
testcase_01 AC 360 ms
28,188 KB
testcase_02 AC 354 ms
28,284 KB
testcase_03 AC 355 ms
28,060 KB
testcase_04 AC 364 ms
28,052 KB
testcase_05 AC 450 ms
51,488 KB
testcase_06 AC 440 ms
51,428 KB
testcase_07 AC 438 ms
51,408 KB
testcase_08 AC 298 ms
17,932 KB
testcase_09 AC 310 ms
22,328 KB
testcase_10 AC 431 ms
40,044 KB
testcase_11 AC 450 ms
51,532 KB
testcase_12 AC 294 ms
19,424 KB
testcase_13 AC 172 ms
6,940 KB
testcase_14 AC 345 ms
26,860 KB
testcase_15 AC 310 ms
21,488 KB
testcase_16 AC 169 ms
8,268 KB
testcase_17 AC 242 ms
16,692 KB
testcase_18 AC 195 ms
17,300 KB
testcase_19 AC 254 ms
11,760 KB
testcase_20 AC 359 ms
27,400 KB
testcase_21 AC 29 ms
4,380 KB
testcase_22 AC 233 ms
16,852 KB
testcase_23 AC 256 ms
19,564 KB
testcase_24 AC 173 ms
15,744 KB
testcase_25 AC 352 ms
27,428 KB
testcase_26 AC 279 ms
20,840 KB
testcase_27 AC 336 ms
24,740 KB
testcase_28 AC 330 ms
22,348 KB
testcase_29 AC 310 ms
24,140 KB
testcase_30 AC 165 ms
11,236 KB
testcase_31 AC 146 ms
5,744 KB
testcase_32 AC 134 ms
12,080 KB
testcase_33 AC 144 ms
11,804 KB
testcase_34 AC 304 ms
21,128 KB
testcase_35 AC 30 ms
4,380 KB
testcase_36 AC 294 ms
22,184 KB
testcase_37 AC 260 ms
17,664 KB
testcase_38 AC 69 ms
6,752 KB
testcase_39 AC 151 ms
12,804 KB
testcase_40 AC 118 ms
4,380 KB
testcase_41 AC 224 ms
11,964 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 325 ms
28,164 KB
testcase_46 AC 326 ms
28,228 KB
testcase_47 AC 326 ms
28,084 KB
testcase_48 AC 324 ms
28,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
//事前処理とUnion-Find
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using ll=long long;
const ll MOD=1e9+7;
struct Union_Find{
    int N;
    vector<int> par;
    Union_Find(int n):N(n){
        par.resize(N);
        for(int i=0;i<N;i++) par[i]=i;
    }
    int root(int X){
        if(par[X]==X) return X;
        return par[X]=root(par[X]);
    }
    bool same(int X,int Y){
        return root(X)==root(Y);
    }
    void unite(int X,int Y){
        X=root(X),Y=root(Y);
        if(X!=Y) par[X]=Y;
    }
};
ll modpow(ll X,ll Y){
    ll ret=1;
    while(Y>0){
        if(Y%2){
            ret*=X;
            ret%=MOD;
        }
        Y/=2;
        X*=X;
        X%=MOD;
    }
    return ret;
}
void dfs(int N,int X,int now,vector<bool> &seen,vector<vector<int>> &edge,vector<vector<int>> &weight,vector<int> &subtree,ll &ans){
    seen[now]=1;
    for(int i=0;i<edge[now].size();i++){
        int next=edge[now][i];
        if(!seen[next]){
            dfs(N,X,next,seen,edge,weight,subtree,ans);
            subtree[now]+=subtree[next];
            ans+=modpow(X,weight[now][i])*subtree[next]%MOD*(N-subtree[next])%MOD;
            ans%=MOD;
        }
    }
    subtree[now]++;
}
int main(){
    int N,M,X; cin>>N>>M>>X;
    ll max=2e5,max2=1e9;;
    assert(2<=N&&N<=max&&N-1<=M&&M<=max&&2<=X&&X<=max2);
    vector<vector<int>> edge(N),weight(N);
    Union_Find UF(N);
    //最小全域木を作る
    ll memo=-1;
    for(int i=0;i<M;i++){
        int x,y,z; cin>>x>>y>>z;
        assert(1<=x&&x<=N&&1<=y&&y<=N&&0<=z&&z<=max2);
        assert(memo<z);
        memo=z;
        if(!UF.same(x-1,y-1)){
            UF.unite(x-1,y-1);
            edge[x-1].push_back(y-1);
            edge[y-1].push_back(x-1);
            weight[x-1].push_back(z);
            weight[y-1].push_back(z);
        }
    }
    {
        std::set<int> memo;
        for(int i=0;i<N;i++) memo.insert(UF.root(i));
        assert(memo.size()==1);
    }
    //dfsで各辺が経路になる回数を数えていく
    vector<int> subtree(N);
    vector<bool> seen(N);
    ll ans=0;
    dfs(N,X,0,seen,edge,weight,subtree,ans);
    cout<<ans<<endl;
}
0