結果

問題 No.1207 グラフX
ユーザー tonyu0tonyu0
提出日時 2020-08-30 14:49:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,443 bytes
コンパイル時間 1,284 ms
コンパイル使用メモリ 121,844 KB
実行使用メモリ 43,676 KB
最終ジャッジ日時 2024-04-27 07:43:05
合計ジャッジ時間 13,220 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 244 ms
25,172 KB
testcase_01 AC 257 ms
25,668 KB
testcase_02 AC 243 ms
25,864 KB
testcase_03 AC 257 ms
25,148 KB
testcase_04 AC 265 ms
25,220 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 223 ms
18,300 KB
testcase_09 AC 214 ms
21,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 211 ms
18,880 KB
testcase_13 AC 150 ms
9,152 KB
testcase_14 AC 234 ms
24,404 KB
testcase_15 AC 216 ms
21,108 KB
testcase_16 AC 140 ms
10,188 KB
testcase_17 AC 175 ms
16,272 KB
testcase_18 AC 135 ms
15,760 KB
testcase_19 AC 200 ms
13,708 KB
testcase_20 AC 257 ms
26,120 KB
testcase_21 AC 29 ms
6,940 KB
testcase_22 AC 173 ms
16,196 KB
testcase_23 AC 186 ms
19,544 KB
testcase_24 AC 122 ms
14,380 KB
testcase_25 AC 241 ms
24,528 KB
testcase_26 AC 194 ms
19,216 KB
testcase_27 AC 232 ms
23,648 KB
testcase_28 AC 217 ms
23,436 KB
testcase_29 AC 219 ms
22,892 KB
testcase_30 AC 125 ms
11,468 KB
testcase_31 AC 136 ms
8,148 KB
testcase_32 AC 98 ms
11,544 KB
testcase_33 AC 111 ms
11,320 KB
testcase_34 AC 208 ms
20,852 KB
testcase_35 AC 29 ms
6,944 KB
testcase_36 AC 211 ms
20,884 KB
testcase_37 AC 192 ms
17,180 KB
testcase_38 AC 56 ms
6,988 KB
testcase_39 AC 134 ms
13,008 KB
testcase_40 AC 118 ms
7,508 KB
testcase_41 AC 183 ms
14,008 KB
testcase_42 AC 1 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 1 ms
6,940 KB
testcase_45 AC 231 ms
25,108 KB
testcase_46 AC 222 ms
24,980 KB
testcase_47 AC 217 ms
25,104 KB
testcase_48 AC 214 ms
25,104 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]) * 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