結果

問題 No.1207 グラフX
ユーザー tonyu0tonyu0
提出日時 2020-08-30 14:54:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 357 ms / 2,000 ms
コード長 2,449 bytes
コンパイル時間 1,460 ms
コンパイル使用メモリ 119,040 KB
実行使用メモリ 41,988 KB
最終ジャッジ日時 2023-08-09 12:44:32
合計ジャッジ時間 14,780 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 277 ms
24,824 KB
testcase_01 AC 278 ms
24,988 KB
testcase_02 AC 277 ms
25,064 KB
testcase_03 AC 285 ms
24,848 KB
testcase_04 AC 277 ms
24,888 KB
testcase_05 AC 357 ms
41,624 KB
testcase_06 AC 350 ms
41,748 KB
testcase_07 AC 326 ms
41,988 KB
testcase_08 AC 228 ms
18,872 KB
testcase_09 AC 236 ms
22,480 KB
testcase_10 AC 340 ms
34,508 KB
testcase_11 AC 344 ms
41,724 KB
testcase_12 AC 226 ms
18,212 KB
testcase_13 AC 158 ms
8,684 KB
testcase_14 AC 262 ms
24,448 KB
testcase_15 AC 240 ms
20,028 KB
testcase_16 AC 146 ms
9,308 KB
testcase_17 AC 187 ms
16,172 KB
testcase_18 AC 148 ms
15,492 KB
testcase_19 AC 207 ms
13,712 KB
testcase_20 AC 272 ms
24,904 KB
testcase_21 AC 30 ms
4,380 KB
testcase_22 AC 188 ms
17,572 KB
testcase_23 AC 206 ms
19,076 KB
testcase_24 AC 133 ms
13,988 KB
testcase_25 AC 272 ms
24,612 KB
testcase_26 AC 213 ms
20,800 KB
testcase_27 AC 255 ms
24,100 KB
testcase_28 AC 244 ms
21,208 KB
testcase_29 AC 233 ms
22,872 KB
testcase_30 AC 132 ms
11,084 KB
testcase_31 AC 140 ms
7,428 KB
testcase_32 AC 103 ms
11,292 KB
testcase_33 AC 114 ms
11,096 KB
testcase_34 AC 233 ms
22,412 KB
testcase_35 AC 30 ms
4,380 KB
testcase_36 AC 240 ms
20,828 KB
testcase_37 AC 205 ms
17,532 KB
testcase_38 AC 56 ms
6,804 KB
testcase_39 AC 114 ms
12,680 KB
testcase_40 AC 120 ms
7,752 KB
testcase_41 AC 185 ms
13,308 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 249 ms
25,092 KB
testcase_46 AC 241 ms
24,932 KB
testcase_47 AC 241 ms
26,544 KB
testcase_48 AC 247 ms
25,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

constexpr ll MOD = 1000000007;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int find(int x) {
    if (par[x] < 0) return x;
    int r = find(par[x]);
    return par[x] = r;
  }
  int sizeOf(int x) { return -par[find(x)]; }
};

struct Edge {
  int from, to;
  int64_t cost;
  Edge(int f, int t, int64_t c) : from(f), to(t), cost(c) {}
  bool operator<(const Edge& e) const { return cost < e.cost; }
};
using Edges = vector<Edge>;

Edges kruskal(Edges& es, int n) {
  UnionFind uf(n);
  sort(es.begin(), es.end());
  Edges mst;

  for (Edge e : es) {
    if (!uf.same(e.from, e.to)) {
      uf.unite(e.from, e.to);
      mst.push_back(e);
    }
  }
  return mst;
}

ll mpow(ll x, ll e) {
  ll res = 1;
  while (e) {
    if (e & 1) res = res * x % MOD;
    x = x * x % MOD;
    e >>= 1;
  }
  return res;
}

ll dfs1(int v, int p, vector<ll>& d, vector<vector<pair<int, ll>>>& g) {
  ll res = 1;
  for (auto e : g[v]) {
    int nv = e.first;
    if (nv == p) continue;
    res += dfs1(nv, v, d, g);
  }
  d[v] = res;
  return res;
}

ll ans = 0;
ll n, m, x;
void dfs2(int v, int p, vector<ll>& d, vector<vector<pair<int, ll>>>& g) {
  for (auto e : g[v]) {
    int nv = e.first;
    if (nv == p) continue;
    ans = (ans + d[nv] * (n - d[nv]) % MOD * mpow(x, e.second) % MOD) % MOD;
    dfs2(nv, v, d, g);
  }
}

int main() {
  ll a, b, c;
  cin >> n >> m >> x;
  Edges es;
  rep(i, 0, m) {
    cin >> a >> b >> c;
    --a, --b;
    es.emplace_back(a, b, c);
  }
  auto mst = kruskal(es, n);
  vector<vector<pair<int, ll>>> g(n);
  rep(i, 0, mst.size()) {
    int from = mst[i].from;
    int to = mst[i].to;
    ll cost = mst[i].cost;
    g[from].emplace_back(to, cost);
    g[to].emplace_back(from, cost);
  }
  vector<ll> d(n);
  dfs1(0, 0, d, g);
  dfs2(0, 0, d, g);
  cout << ans << endl;
}
0