結果

問題 No.1207 グラフX
ユーザー yukudoyukudo
提出日時 2020-09-06 17:39:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 3,376 bytes
コンパイル時間 3,201 ms
コンパイル使用メモリ 172,452 KB
実行使用メモリ 33,844 KB
最終ジャッジ日時 2023-08-19 14:10:55
合計ジャッジ時間 12,193 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
18,648 KB
testcase_01 AC 141 ms
18,920 KB
testcase_02 AC 131 ms
18,576 KB
testcase_03 AC 134 ms
18,612 KB
testcase_04 AC 137 ms
18,652 KB
testcase_05 AC 183 ms
33,488 KB
testcase_06 AC 192 ms
33,844 KB
testcase_07 AC 201 ms
33,540 KB
testcase_08 AC 115 ms
12,464 KB
testcase_09 AC 122 ms
15,552 KB
testcase_10 AC 178 ms
26,496 KB
testcase_11 AC 182 ms
33,488 KB
testcase_12 AC 108 ms
14,064 KB
testcase_13 AC 62 ms
6,180 KB
testcase_14 AC 130 ms
17,920 KB
testcase_15 AC 116 ms
14,900 KB
testcase_16 AC 60 ms
7,172 KB
testcase_17 AC 93 ms
11,928 KB
testcase_18 AC 78 ms
12,224 KB
testcase_19 AC 89 ms
8,956 KB
testcase_20 AC 145 ms
18,304 KB
testcase_21 AC 12 ms
4,384 KB
testcase_22 AC 98 ms
12,200 KB
testcase_23 AC 100 ms
14,292 KB
testcase_24 AC 67 ms
11,356 KB
testcase_25 AC 138 ms
18,244 KB
testcase_26 AC 100 ms
14,636 KB
testcase_27 AC 126 ms
16,576 KB
testcase_28 AC 116 ms
15,556 KB
testcase_29 AC 120 ms
16,644 KB
testcase_30 AC 61 ms
8,700 KB
testcase_31 AC 53 ms
5,152 KB
testcase_32 AC 50 ms
9,256 KB
testcase_33 AC 54 ms
8,920 KB
testcase_34 AC 112 ms
14,480 KB
testcase_35 AC 13 ms
4,400 KB
testcase_36 AC 110 ms
15,452 KB
testcase_37 AC 94 ms
12,584 KB
testcase_38 AC 26 ms
6,088 KB
testcase_39 AC 56 ms
9,924 KB
testcase_40 AC 44 ms
4,856 KB
testcase_41 AC 82 ms
9,400 KB
testcase_42 AC 2 ms
4,384 KB
testcase_43 AC 1 ms
4,508 KB
testcase_44 AC 1 ms
4,384 KB
testcase_45 AC 127 ms
18,780 KB
testcase_46 AC 128 ms
18,656 KB
testcase_47 AC 148 ms
18,744 KB
testcase_48 AC 126 ms
18,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
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;}
#ifdef LOCAL
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cerr<<(*i)<<" ";cerr<<endl;}
#else
template<class T>void pv(T a,T b){}
#endif

ll nextLong() { ll x; scanf("%lld", &x); return x;}

const ll MOD = 1e9 + 7;

struct mint {
  ll x;
  mint(ll x=0):x((x%MOD+MOD)%MOD){}
  mint& operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}
  mint& operator-=(const mint a) {if ((x += MOD-a.x) >= MOD) x -= MOD;return *this;}
  mint& operator*=(const mint a) {(x *= a.x) %= MOD;return *this;}
  mint operator+(const mint a) const {mint res(*this);return res+=a;}
  mint operator-(const mint a) const {mint res(*this);return res-=a;}
  mint operator*(const mint a) const {mint res(*this);return res*=a;}
  mint pow(ll b) const {
    mint res(1), a(*this);
    while (b) {
      if (b & 1) res *= a;
      a *= a;
      b >>= 1;
    }
    return res;
  }
  // for prime MOD
  mint inv() const {return pow(MOD-2);}
  mint& operator/=(const mint a) {return (*this) *= a.inv();}
  mint operator/(const mint a) const {mint res(*this);return res/=a;}
};
ostream& operator<<(ostream& os, const mint& a) {os << a.x; return os;}

struct UnionFind {
  vector<int> data; // 根の場合は集合の大きさ*-1、子の場合は親の番号が入る
  UnionFind(int size) : data(size, -1) { }
  bool link(int x, int y) { //新たな併合を行うとtrue
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  int root(int x) { // 代表元を返す
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) { // 要素xが含まれる集合の大きさ
    return -data[root(x)];
  }
  bool same(int x, int y) { // 同じ集合ならtrue
    return root(x) == root(y);
  }
  int num() { // 異なる集合の数
    int res = 0;
    REP(i, data.size()) res += root(i) == i;
    return res;
  }
};


struct Edge {
  int a, b, z;
};

vector<Edge> es;
vector< vector<int> > g;


const int MAX_N = 212345;
int sz[MAX_N];

int dfs(int cur, int prev) {
  int res = 1;
  for (int eid : g[cur]) {
    int nxt = es[eid].a ^ es[eid].b ^ cur;
    if (nxt == prev) continue;

    int c = dfs(nxt, cur);
    sz[eid] = c;
    res += c;
  }
  return res;
}


int main2() {
  int N = nextLong();
  int M = nextLong();
  ll X = nextLong();

  UnionFind uf(N);
  es.clear();
  g = vector< vector<int> >(N);
  REP(i, M) {
    int a = nextLong() - 1;
    int b = nextLong() - 1;
    int z = nextLong();
    if (uf.link(a, b)) {
      g[a].push_back(es.size());
      g[b].push_back(es.size());
      es.push_back((Edge){a, b, z});
    }
  }

  CLR(sz, 0);
  dfs(0, -1);

  mint ans = 0;
  for (int i = 0; i < (int)es.size(); i++) {
    ans += mint(sz[i]) * mint(N - sz[i]) * mint(X).pow(es[i].z);
  }
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}
0