結果

問題 No.1207 グラフX
ユーザー umezoumezo
提出日時 2022-08-12 17:51:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 779 ms / 2,000 ms
コード長 1,652 bytes
コンパイル時間 5,719 ms
コンパイル使用メモリ 218,640 KB
実行使用メモリ 79,940 KB
最終ジャッジ日時 2023-10-24 03:48:08
合計ジャッジ時間 32,689 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 742 ms
46,264 KB
testcase_01 AC 742 ms
46,292 KB
testcase_02 AC 757 ms
46,368 KB
testcase_03 AC 779 ms
46,300 KB
testcase_04 AC 756 ms
46,276 KB
testcase_05 AC 636 ms
79,940 KB
testcase_06 AC 666 ms
79,940 KB
testcase_07 AC 633 ms
79,940 KB
testcase_08 AC 467 ms
30,880 KB
testcase_09 AC 579 ms
37,360 KB
testcase_10 AC 634 ms
64,364 KB
testcase_11 AC 640 ms
79,940 KB
testcase_12 AC 499 ms
33,124 KB
testcase_13 AC 124 ms
14,204 KB
testcase_14 AC 712 ms
44,612 KB
testcase_15 AC 573 ms
36,236 KB
testcase_16 AC 157 ms
16,052 KB
testcase_17 AC 395 ms
29,084 KB
testcase_18 AC 366 ms
29,672 KB
testcase_19 AC 291 ms
21,596 KB
testcase_20 AC 718 ms
45,564 KB
testcase_21 AC 14 ms
8,668 KB
testcase_22 AC 387 ms
29,504 KB
testcase_23 AC 470 ms
33,140 KB
testcase_24 AC 333 ms
27,276 KB
testcase_25 AC 726 ms
45,036 KB
testcase_26 AC 523 ms
35,404 KB
testcase_27 AC 648 ms
40,756 KB
testcase_28 AC 580 ms
37,468 KB
testcase_29 AC 624 ms
40,644 KB
testcase_30 AC 237 ms
20,804 KB
testcase_31 AC 88 ms
12,092 KB
testcase_32 AC 229 ms
21,860 KB
testcase_33 AC 247 ms
21,332 KB
testcase_34 AC 560 ms
35,884 KB
testcase_35 AC 17 ms
9,048 KB
testcase_36 AC 577 ms
37,040 KB
testcase_37 AC 444 ms
30,632 KB
testcase_38 AC 87 ms
13,676 KB
testcase_39 AC 262 ms
22,756 KB
testcase_40 AC 46 ms
9,380 KB
testcase_41 AC 286 ms
21,860 KB
testcase_42 AC 4 ms
8,428 KB
testcase_43 AC 4 ms
8,428 KB
testcase_44 AC 4 ms
8,428 KB
testcase_45 AC 722 ms
46,252 KB
testcase_46 AC 711 ms
46,252 KB
testcase_47 AC 721 ms
46,252 KB
testcase_48 AC 746 ms
46,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

vector<int> G[200200];
class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int findSet(int x){
    if(x != p[x]) p[x]=findSet(p[x]);
    return p[x];
  }
};

const int MOD=1e9+7;
ll modpow(ll x,ll n){
  x%=MOD;
  ll ans=1;
  while(n){
    if(n&1) ans=ans*x%MOD;
    x=x*x%MOD;
    n/=2;
  }
  return ans;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll n,m,x;
  cin>>n>>m>>x;
  Dis ds=Dis(n);
  map<pair<int,int>,ll> M;
  rep(i,m){
    ll x,y,z;
    cin>>x>>y>>z;
    x--,y--;
    if(!ds.same(x,y)){
      G[x].push_back(y);
      G[y].push_back(x);
      M[{x,y}]=z;
      M[{y,x}]=z;
      ds.unite(x,y);
    }
  }
  
  ll ans=0;
  vector<ll> chi(n);
  auto dfs=[&](auto dfs,int v,int p)->void{
    ll tmp=0;
    for(auto nv:G[v]){
      if(nv==p) continue;
      dfs(dfs,nv,v);
      tmp+=chi[nv];
      ans=(ans+chi[nv]*(n-chi[nv])%MOD*modpow(x,M[{v,nv}])%MOD)%MOD;
    }
    chi[v]=tmp+1;
  };
  dfs(dfs,0,-1);
  cout<<ans<<endl;
  
  return 0;
}
0