結果

問題 No.1207 グラフX
ユーザー snow39snow39
提出日時 2020-08-30 14:46:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 2,439 bytes
コンパイル時間 1,341 ms
コンパイル使用メモリ 110,348 KB
実行使用メモリ 38,660 KB
最終ジャッジ日時 2024-04-27 07:40:47
合計ジャッジ時間 10,520 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 199 ms
22,828 KB
testcase_01 AC 193 ms
22,832 KB
testcase_02 AC 170 ms
22,852 KB
testcase_03 AC 174 ms
22,840 KB
testcase_04 AC 182 ms
22,828 KB
testcase_05 AC 251 ms
38,660 KB
testcase_06 AC 260 ms
38,660 KB
testcase_07 AC 262 ms
38,532 KB
testcase_08 AC 135 ms
16,204 KB
testcase_09 AC 149 ms
19,500 KB
testcase_10 AC 268 ms
31,360 KB
testcase_11 AC 257 ms
38,656 KB
testcase_12 AC 141 ms
16,940 KB
testcase_13 AC 67 ms
8,576 KB
testcase_14 AC 190 ms
21,888 KB
testcase_15 AC 153 ms
18,200 KB
testcase_16 AC 67 ms
9,088 KB
testcase_17 AC 116 ms
14,544 KB
testcase_18 AC 102 ms
14,144 KB
testcase_19 AC 106 ms
12,288 KB
testcase_20 AC 185 ms
22,436 KB
testcase_21 AC 12 ms
6,944 KB
testcase_22 AC 119 ms
14,860 KB
testcase_23 AC 126 ms
16,468 KB
testcase_24 AC 79 ms
13,100 KB
testcase_25 AC 175 ms
22,244 KB
testcase_26 AC 147 ms
17,384 KB
testcase_27 AC 176 ms
20,276 KB
testcase_28 AC 163 ms
19,768 KB
testcase_29 AC 161 ms
19,900 KB
testcase_30 AC 71 ms
10,496 KB
testcase_31 AC 57 ms
7,424 KB
testcase_32 AC 62 ms
10,400 KB
testcase_33 AC 67 ms
10,404 KB
testcase_34 AC 141 ms
18,036 KB
testcase_35 AC 12 ms
6,940 KB
testcase_36 AC 141 ms
19,200 KB
testcase_37 AC 117 ms
15,524 KB
testcase_38 AC 32 ms
6,944 KB
testcase_39 AC 68 ms
11,620 KB
testcase_40 AC 43 ms
6,944 KB
testcase_41 AC 91 ms
12,236 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 164 ms
22,612 KB
testcase_46 AC 171 ms
22,764 KB
testcase_47 AC 175 ms
22,800 KB
testcase_48 AC 169 ms
22,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

class DisjointSet{
public:
  vector<int> rank,p;
  vector<int> sz;
 
  DisjointSet(){}
  DisjointSet(int size){
    rank.resize(size,0);
    p.resize(size,0);
    sz.resize(size,0);
    for(int i=0;i<size;i++) makeSet(i);
  }
 
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
    sz[x]=1;
  }
 
  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;
      sz[x]+=sz[y];
    }else{
      p[x]=y;
      sz[y]+=sz[x];
      if(rank[x]==rank[y]){
        rank[y]++;
      }
    }
  }
 
  int findSet(int x){
    if(x!=p[x]){
      p[x]=findSet(p[x]);
    }
    return p[x];
  }
 
  int findSize(int x){
    return sz[findSet(x)];
  }
};

ll mod_pow(ll x,ll n){
  x%=MOD;
  ll res=1;
  while(n>0){
    if(n&1) res=res*x%MOD;
    x=x*x%MOD;
    n>>=1;
  }
  return res;
}

void add(ll &a,ll b){
  a=(a+b)%MOD;
}

void mul(ll &a,ll b){
  a%=MOD;b%=MOD;
  a=a*b%MOD;
}

vector<vector<pair<int,ll>>> g;
vector<ll> sz;

void szdfs(int now,int par){
  sz[now]=1;
  for(auto p:g[now]){
    int nex=p.first;
    if(nex==par) continue;
    szdfs(nex,now);
    sz[now]+=sz[nex];
  }
}

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

  int N,M;
  ll X;
  cin>>N>>M>>X;
  vector<int> A(M),B(M);
  vector<ll> C(M);
  rep(i,M) cin>>A[i]>>B[i]>>C[i];

  rep(i,M){
    A[i]--;B[i]--;
  }

  g.resize(N);
  {
    DisjointSet us(N);
    rep(i,M){
      if(us.same(A[i],B[i])) continue;
      us.unite(A[i],B[i]);
      g[A[i]].push_back(mkp(B[i],C[i]));
      g[B[i]].push_back(mkp(A[i],C[i]));
    }
  }
  sz.resize(N,0);
  szdfs(0,-1);

  ll ans=0;
  rep(i,N) for(auto p:g[i]){
    int to=p.first;
    ll z=p.second;
    if(i>to) continue;
    ll num=min(sz[i],sz[to])*(N-min(sz[i],sz[to]));
    ll val=mod_pow(X,z);
    mul(val,num);
    add(ans,val);
  }

  cout<<ans<<endl;


  return 0;
}
0