結果

問題 No.1207 グラフX
ユーザー hs0319boyhs0319boy
提出日時 2020-08-30 15:42:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 681 ms / 2,000 ms
コード長 2,484 bytes
コンパイル時間 1,810 ms
コンパイル使用メモリ 182,064 KB
実行使用メモリ 42,352 KB
最終ジャッジ日時 2024-04-27 09:08:02
合計ジャッジ時間 26,575 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 662 ms
30,760 KB
testcase_01 AC 666 ms
30,836 KB
testcase_02 AC 677 ms
30,556 KB
testcase_03 AC 670 ms
30,592 KB
testcase_04 AC 681 ms
30,572 KB
testcase_05 AC 637 ms
42,352 KB
testcase_06 AC 629 ms
42,352 KB
testcase_07 AC 625 ms
42,236 KB
testcase_08 AC 462 ms
22,736 KB
testcase_09 AC 530 ms
25,980 KB
testcase_10 AC 607 ms
36,536 KB
testcase_11 AC 630 ms
42,232 KB
testcase_12 AC 474 ms
23,864 KB
testcase_13 AC 206 ms
14,144 KB
testcase_14 AC 662 ms
29,828 KB
testcase_15 AC 536 ms
25,360 KB
testcase_16 AC 217 ms
15,252 KB
testcase_17 AC 410 ms
21,676 KB
testcase_18 AC 359 ms
22,144 KB
testcase_19 AC 345 ms
17,832 KB
testcase_20 AC 672 ms
30,296 KB
testcase_21 AC 36 ms
11,288 KB
testcase_22 AC 386 ms
21,928 KB
testcase_23 AC 436 ms
23,952 KB
testcase_24 AC 293 ms
20,896 KB
testcase_25 AC 667 ms
30,068 KB
testcase_26 AC 487 ms
25,112 KB
testcase_27 AC 584 ms
27,920 KB
testcase_28 AC 542 ms
26,156 KB
testcase_29 AC 563 ms
27,748 KB
testcase_30 AC 240 ms
17,604 KB
testcase_31 AC 174 ms
13,124 KB
testcase_32 AC 215 ms
18,188 KB
testcase_33 AC 225 ms
17,728 KB
testcase_34 AC 507 ms
25,244 KB
testcase_35 AC 38 ms
11,328 KB
testcase_36 AC 530 ms
25,752 KB
testcase_37 AC 432 ms
22,676 KB
testcase_38 AC 95 ms
13,852 KB
testcase_39 AC 240 ms
18,744 KB
testcase_40 AC 135 ms
11,560 KB
testcase_41 AC 320 ms
18,088 KB
testcase_42 AC 7 ms
11,072 KB
testcase_43 AC 6 ms
11,008 KB
testcase_44 AC 6 ms
11,168 KB
testcase_45 AC 658 ms
30,560 KB
testcase_46 AC 650 ms
30,560 KB
testcase_47 AC 666 ms
30,560 KB
testcase_48 AC 663 ms
30,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const ll inf=1e9+7;
const ll INF=1e18;
const ll mod=998244353;

int MAX_N=2e5+20;

vin par(MAX_N);
vin deep(MAX_N);
vin sizee(MAX_N);
void init(int n);
int root(int x);
void unite(int x,int y);
bool same(int x,int y);
int group_size(int x);

vvin baby(MAX_N);
vin babynum(MAX_N,1);

void babycount(int v,int p){//自身を含む子孫の数
    if(baby[v].size()==0)return;
    for(auto b:baby[v]){
        if(b==p)continue;
        babycount(b,v);
        babynum[v]+=babynum[b];
    }
}

ll modpow(ll a,ll n,ll m=inf){
    ll res=1;
    while(n){
        if(n&1)res=res*a%m;
        a=a*a%m;
        n>>=1;
    }
    return res;
}

ll fermat(ll a,ll b,ll m){
    return (a%m)*modpow(b,m-2,m)%m;
}

int main(){
    ll n,m,X;cin>>n>>m>>X;
    init(n);
    map<pair<int,int>,ll> road;
    int x,y;ll z;
    rep(i,m){
        cin>>x>>y>>z;
        if(same(x,y))continue;
        unite(x,y);
        road[mp(min(x,y),max(x,y))]=z;
        baby[x].push_back(y);
        baby[y].push_back(x);
    }
    babycount(1,-1);
    ll ans=(ll)0,tmp;
    rep2(i,1,n+1){
        for(auto b:baby[i]){
            tmp=min(babynum[i],babynum[b]);
            ans+=modpow(X,road[mp(min(i,b),max(i,b))])*((tmp*(n-tmp))%inf);
            ans%=inf;
        }
    }
    cout<<fermat(ans,(ll)2,inf)<<endl;
}

void init(int n){
    rep(i,n+1){
        par[i]=i;
        deep[i]=0;
        sizee[i]=1;
    }
}
int root(int x){
    if(par[x]==x)return x;
    else return par[x]=root(par[x]);
}
void unite(int x,int y){
    x=root(x);
    y=root(y);
    if(x==y)return;
    if(deep[x]<deep[y]){
        par[x]=y;
        sizee[y]+=sizee[x];
    }
    else{
        par[y]=x;
        sizee[x]+=sizee[y];
        if(deep[x]==deep[y])deep[x]++;
    }
}
bool same(int x,int y){
    return root(x)==root(y);
}
int group_size(int x){
    return sizee[root(x)];
}
0