結果

問題 No.1207 グラフX
ユーザー penguinmanpenguinman
提出日時 2020-08-08 18:07:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 1,929 bytes
コンパイル時間 1,643 ms
コンパイル使用メモリ 172,956 KB
実行使用メモリ 51,764 KB
最終ジャッジ日時 2024-04-09 21:32:09
合計ジャッジ時間 12,822 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 194 ms
28,332 KB
testcase_01 AC 197 ms
28,340 KB
testcase_02 AC 195 ms
28,372 KB
testcase_03 AC 216 ms
28,220 KB
testcase_04 AC 210 ms
28,208 KB
testcase_05 AC 294 ms
51,760 KB
testcase_06 AC 296 ms
51,728 KB
testcase_07 AC 291 ms
51,660 KB
testcase_08 AC 151 ms
17,984 KB
testcase_09 AC 172 ms
22,304 KB
testcase_10 AC 276 ms
40,260 KB
testcase_11 AC 298 ms
51,764 KB
testcase_12 AC 164 ms
19,540 KB
testcase_13 AC 74 ms
7,168 KB
testcase_14 AC 207 ms
26,900 KB
testcase_15 AC 172 ms
21,516 KB
testcase_16 AC 77 ms
8,448 KB
testcase_17 AC 133 ms
16,768 KB
testcase_18 AC 102 ms
17,536 KB
testcase_19 AC 117 ms
11,776 KB
testcase_20 AC 206 ms
27,816 KB
testcase_21 AC 12 ms
6,944 KB
testcase_22 AC 123 ms
17,180 KB
testcase_23 AC 150 ms
19,652 KB
testcase_24 AC 90 ms
15,616 KB
testcase_25 AC 200 ms
27,480 KB
testcase_26 AC 153 ms
21,008 KB
testcase_27 AC 189 ms
24,788 KB
testcase_28 AC 178 ms
22,396 KB
testcase_29 AC 178 ms
24,508 KB
testcase_30 AC 81 ms
11,648 KB
testcase_31 AC 60 ms
6,940 KB
testcase_32 AC 70 ms
12,288 KB
testcase_33 AC 70 ms
11,904 KB
testcase_34 AC 173 ms
21,312 KB
testcase_35 AC 13 ms
6,940 KB
testcase_36 AC 162 ms
22,276 KB
testcase_37 AC 132 ms
18,020 KB
testcase_38 AC 35 ms
6,940 KB
testcase_39 AC 84 ms
12,800 KB
testcase_40 AC 44 ms
6,940 KB
testcase_41 AC 114 ms
12,416 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 210 ms
28,188 KB
testcase_46 AC 203 ms
28,440 KB
testcase_47 AC 203 ms
28,316 KB
testcase_48 AC 200 ms
28,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
//事前処理とUnion-Find
using std::cin;
using std::cout;
#define endl "\n"
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(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    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