結果

問題 No.1207 グラフX
ユーザー penguinmanpenguinman
提出日時 2020-07-29 13:18:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 460 ms / 2,000 ms
コード長 1,863 bytes
コンパイル時間 1,737 ms
コンパイル使用メモリ 174,360 KB
実行使用メモリ 51,544 KB
最終ジャッジ日時 2023-09-18 05:21:28
合計ジャッジ時間 17,798 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 362 ms
28,012 KB
testcase_01 AC 360 ms
28,108 KB
testcase_02 AC 360 ms
28,288 KB
testcase_03 AC 368 ms
28,108 KB
testcase_04 AC 363 ms
28,044 KB
testcase_05 AC 448 ms
51,544 KB
testcase_06 AC 439 ms
51,376 KB
testcase_07 AC 448 ms
51,420 KB
testcase_08 AC 292 ms
17,868 KB
testcase_09 AC 307 ms
22,312 KB
testcase_10 AC 460 ms
40,092 KB
testcase_11 AC 448 ms
51,468 KB
testcase_12 AC 291 ms
19,360 KB
testcase_13 AC 169 ms
6,928 KB
testcase_14 AC 348 ms
26,768 KB
testcase_15 AC 317 ms
21,252 KB
testcase_16 AC 167 ms
8,572 KB
testcase_17 AC 239 ms
16,536 KB
testcase_18 AC 191 ms
17,088 KB
testcase_19 AC 252 ms
11,712 KB
testcase_20 AC 356 ms
27,380 KB
testcase_21 AC 29 ms
4,376 KB
testcase_22 AC 234 ms
16,816 KB
testcase_23 AC 261 ms
19,444 KB
testcase_24 AC 172 ms
15,552 KB
testcase_25 AC 358 ms
27,352 KB
testcase_26 AC 283 ms
20,684 KB
testcase_27 AC 332 ms
24,560 KB
testcase_28 AC 311 ms
22,340 KB
testcase_29 AC 311 ms
24,340 KB
testcase_30 AC 162 ms
11,168 KB
testcase_31 AC 145 ms
5,676 KB
testcase_32 AC 133 ms
12,232 KB
testcase_33 AC 146 ms
11,740 KB
testcase_34 AC 304 ms
21,000 KB
testcase_35 AC 30 ms
4,376 KB
testcase_36 AC 296 ms
21,952 KB
testcase_37 AC 264 ms
17,684 KB
testcase_38 AC 68 ms
6,680 KB
testcase_39 AC 149 ms
12,884 KB
testcase_40 AC 117 ms
4,376 KB
testcase_41 AC 223 ms
11,972 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 329 ms
28,072 KB
testcase_46 AC 327 ms
28,000 KB
testcase_47 AC 332 ms
28,020 KB
testcase_48 AC 334 ms
28,040 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 int 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;
    vector<vector<int>> edge(N),weight(N);
    Union_Find UF(N);
    //最小全域木を作る
    for(int i=0;i<M;i++){
        int x,y,z; cin>>x>>y>>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);
        }
    }
    //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