結果

問題 No.1207 グラフX
ユーザー tonyu0tonyu0
提出日時 2020-08-30 14:54:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 2,449 bytes
コンパイル時間 1,441 ms
コンパイル使用メモリ 121,344 KB
実行使用メモリ 42,740 KB
最終ジャッジ日時 2024-04-27 07:47:14
合計ジャッジ時間 15,980 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 340 ms
25,804 KB
testcase_01 AC 323 ms
25,020 KB
testcase_02 AC 320 ms
25,296 KB
testcase_03 AC 326 ms
26,192 KB
testcase_04 AC 325 ms
26,376 KB
testcase_05 AC 400 ms
41,932 KB
testcase_06 AC 401 ms
42,064 KB
testcase_07 AC 411 ms
42,740 KB
testcase_08 AC 292 ms
17,952 KB
testcase_09 AC 294 ms
21,396 KB
testcase_10 AC 395 ms
35,492 KB
testcase_11 AC 395 ms
41,792 KB
testcase_12 AC 274 ms
18,868 KB
testcase_13 AC 181 ms
8,940 KB
testcase_14 AC 315 ms
24,576 KB
testcase_15 AC 295 ms
20,312 KB
testcase_16 AC 165 ms
9,860 KB
testcase_17 AC 216 ms
17,616 KB
testcase_18 AC 175 ms
15,700 KB
testcase_19 AC 246 ms
13,208 KB
testcase_20 AC 333 ms
24,732 KB
testcase_21 AC 34 ms
6,944 KB
testcase_22 AC 216 ms
17,168 KB
testcase_23 AC 233 ms
20,592 KB
testcase_24 AC 154 ms
14,204 KB
testcase_25 AC 317 ms
25,284 KB
testcase_26 AC 253 ms
19,520 KB
testcase_27 AC 300 ms
23,376 KB
testcase_28 AC 293 ms
21,628 KB
testcase_29 AC 281 ms
22,956 KB
testcase_30 AC 156 ms
11,468 KB
testcase_31 AC 156 ms
8,400 KB
testcase_32 AC 125 ms
11,416 KB
testcase_33 AC 133 ms
11,448 KB
testcase_34 AC 280 ms
20,016 KB
testcase_35 AC 34 ms
6,944 KB
testcase_36 AC 263 ms
23,364 KB
testcase_37 AC 238 ms
17,348 KB
testcase_38 AC 63 ms
7,088 KB
testcase_39 AC 139 ms
13,004 KB
testcase_40 AC 134 ms
8,272 KB
testcase_41 AC 225 ms
14,636 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 300 ms
25,616 KB
testcase_46 AC 297 ms
25,360 KB
testcase_47 AC 294 ms
25,232 KB
testcase_48 AC 285 ms
25,620 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