結果

問題 No.1207 グラフX
ユーザー beetbeet
提出日時 2020-08-30 13:12:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 3,352 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 212,928 KB
実行使用メモリ 36,432 KB
最終ジャッジ日時 2024-04-27 06:45:43
合計ジャッジ時間 11,726 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 171 ms
20,452 KB
testcase_01 AC 169 ms
20,552 KB
testcase_02 AC 170 ms
20,496 KB
testcase_03 AC 171 ms
20,484 KB
testcase_04 AC 174 ms
20,472 KB
testcase_05 AC 251 ms
36,384 KB
testcase_06 AC 251 ms
36,432 KB
testcase_07 AC 237 ms
36,388 KB
testcase_08 AC 134 ms
13,632 KB
testcase_09 AC 151 ms
17,352 KB
testcase_10 AC 239 ms
29,672 KB
testcase_11 AC 253 ms
36,432 KB
testcase_12 AC 140 ms
14,604 KB
testcase_13 AC 63 ms
6,944 KB
testcase_14 AC 173 ms
19,668 KB
testcase_15 AC 147 ms
15,956 KB
testcase_16 AC 63 ms
7,040 KB
testcase_17 AC 105 ms
12,620 KB
testcase_18 AC 87 ms
12,900 KB
testcase_19 AC 98 ms
9,472 KB
testcase_20 AC 174 ms
20,112 KB
testcase_21 AC 11 ms
6,948 KB
testcase_22 AC 102 ms
12,972 KB
testcase_23 AC 122 ms
14,580 KB
testcase_24 AC 82 ms
11,888 KB
testcase_25 AC 159 ms
19,920 KB
testcase_26 AC 125 ms
15,504 KB
testcase_27 AC 157 ms
17,836 KB
testcase_28 AC 151 ms
17,452 KB
testcase_29 AC 153 ms
17,816 KB
testcase_30 AC 81 ms
8,984 KB
testcase_31 AC 54 ms
6,940 KB
testcase_32 AC 63 ms
9,328 KB
testcase_33 AC 67 ms
9,304 KB
testcase_34 AC 151 ms
15,648 KB
testcase_35 AC 11 ms
6,944 KB
testcase_36 AC 148 ms
16,944 KB
testcase_37 AC 126 ms
13,424 KB
testcase_38 AC 29 ms
6,940 KB
testcase_39 AC 72 ms
10,448 KB
testcase_40 AC 40 ms
6,940 KB
testcase_41 AC 98 ms
9,784 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 160 ms
20,404 KB
testcase_46 AC 158 ms
20,412 KB
testcase_47 AC 158 ms
20,372 KB
testcase_48 AC 157 ms
20,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,Int k){
    Mint num(1),dom(1);
    for(Int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


struct UnionFind{
  Int num;
  vector<Int> rs,ps;
  UnionFind(){}
  UnionFind(Int n):num(n),rs(n,1),ps(n,0){iota(ps.begin(),ps.end(),0);}
  Int find(Int x){
    return (x==ps[x]?x:ps[x]=find(ps[x]));
  }
  bool same(Int x,Int y){
    return find(x)==find(y);
  }
  void unite(Int x,Int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(rs[x]<rs[y]) swap(x,y);
    rs[x]+=rs[y];
    ps[y]=x;
    num--;
  }
  Int size(Int x){
    return rs[find(x)];
  }
  Int count() const{
    return num;
  }
};


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m,x;
  cin>>n>>m>>x;

  UnionFind uf(n);
  using P = pair<Int, Int>;
  vector<vector<P>> G(n);
  for(Int i=0;i<m;i++){
    Int a,b,c;
    cin>>a>>b>>c;
    a--;b--;
    if(uf.same(a,b)) continue;
    uf.unite(a,b);
    G[a].emplace_back(b,c);
    G[b].emplace_back(a,c);
  }

  using M = Mint<Int>;
  M ans{0};
  MFP([&](auto dfs,Int v,Int p)->Int{
    Int res=1;
    for(auto[u,c]:G[v]){
      if(u==p) continue;
      Int tmp=dfs(u,v);
      res+=tmp;
      ans+=M(n-tmp)*M(tmp)*M(x).pow(c);
    }
    return res;
  })(0,-1);

  cout<<ans<<newl;
  return 0;
}
0