結果

問題 No.1207 グラフX
ユーザー risujirohrisujiroh
提出日時 2020-08-30 13:14:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 4,042 bytes
コンパイル時間 3,082 ms
コンパイル使用メモリ 271,416 KB
実行使用メモリ 43,316 KB
最終ジャッジ日時 2023-08-09 11:39:40
合計ジャッジ時間 12,607 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 194 ms
29,164 KB
testcase_01 AC 201 ms
29,456 KB
testcase_02 AC 191 ms
29,172 KB
testcase_03 AC 191 ms
29,424 KB
testcase_04 AC 198 ms
29,812 KB
testcase_05 AC 224 ms
43,316 KB
testcase_06 AC 233 ms
43,220 KB
testcase_07 AC 232 ms
43,272 KB
testcase_08 AC 140 ms
18,408 KB
testcase_09 AC 156 ms
23,532 KB
testcase_10 AC 227 ms
36,152 KB
testcase_11 AC 227 ms
43,144 KB
testcase_12 AC 140 ms
20,512 KB
testcase_13 AC 61 ms
7,240 KB
testcase_14 AC 179 ms
28,136 KB
testcase_15 AC 157 ms
22,792 KB
testcase_16 AC 66 ms
8,640 KB
testcase_17 AC 123 ms
17,204 KB
testcase_18 AC 102 ms
17,624 KB
testcase_19 AC 101 ms
12,540 KB
testcase_20 AC 194 ms
28,744 KB
testcase_21 AC 11 ms
4,376 KB
testcase_22 AC 116 ms
17,740 KB
testcase_23 AC 137 ms
20,568 KB
testcase_24 AC 89 ms
16,220 KB
testcase_25 AC 192 ms
28,460 KB
testcase_26 AC 147 ms
22,016 KB
testcase_27 AC 172 ms
25,680 KB
testcase_28 AC 154 ms
24,228 KB
testcase_29 AC 156 ms
25,176 KB
testcase_30 AC 69 ms
11,892 KB
testcase_31 AC 51 ms
5,716 KB
testcase_32 AC 63 ms
12,728 KB
testcase_33 AC 68 ms
12,080 KB
testcase_34 AC 149 ms
22,008 KB
testcase_35 AC 12 ms
4,376 KB
testcase_36 AC 151 ms
23,336 KB
testcase_37 AC 128 ms
18,564 KB
testcase_38 AC 29 ms
6,980 KB
testcase_39 AC 72 ms
13,596 KB
testcase_40 AC 40 ms
4,380 KB
testcase_41 AC 95 ms
12,560 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 178 ms
29,328 KB
testcase_46 AC 176 ms
29,300 KB
testcase_47 AC 181 ms
29,200 KB
testcase_48 AC 178 ms
29,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>

#ifndef DUMP
#define DUMP(...) void(0)
#endif

using namespace std;

template <class T, class Op = multiplies<>>
constexpr T power(T a, uint64_t n, T init = 1, Op op = Op{}) {
  while (n) {
    if (n & 1) init = op(init, a);
    if (n >>= 1) a = op(a, a);
  }
  return init;
}

template <uint32_t Mod>
struct modular {
  using T = modular;
  static constexpr uint32_t mod = Mod;
  uint32_t v;
  modular(int64_t x = 0) : v((x %= mod) < 0 ? x + mod : x) {}
  T operator-() const { return T() - *this; }
  T& operator+=(T b) { return v += int(v += b.v - mod) < 0 ? mod : 0, *this; }
  T& operator-=(T b) { return v += int(v -= b.v) < 0 ? mod : 0, *this; }
  T& operator*=(T b) { return v = uint64_t(v) * b.v % mod, *this; }
  T& operator/=(T b) { return *this *= power(b, mod - 2); }
  friend T operator+(T a, T b) { return a += b; }
  friend T operator-(T a, T b) { return a -= b; }
  friend T operator*(T a, T b) { return a *= b; }
  friend T operator/(T a, T b) { return a /= b; }
  friend bool operator==(T a, T b) { return a.v == b.v; }
};

struct dsu {
  int cc;
  vector<int> p, sz;
  dsu(int n = 0) : cc(n), p(n, -1), sz(n, 1) {}
  int root(int v) {
    if (p[v] == -1) return v;
    return p[v] = root(p[v]);
  }
  bool unite(int u, int v) {
    u = root(u), v = root(v);
    if (u == v) return false;
    --cc;
    if (sz[u] < sz[v]) swap(u, v);
    p[v] = u;
    sz[u] += sz[v];
    return true;
  }
  bool same(int u, int v) { return root(u) == root(v); }
  int size(int v) { return sz[root(v)]; }
};

struct graph {
  struct edge {
    int src, dst, cost;
    int operator-(int v) const { return src ^ dst ^ v; }
  };
  int n, m;
  vector<edge> edges;
  vector<vector<pair<int, int>>> adj;
  graph(int _n = 0) : n(_n), m(0), adj(n) {}
  int add(const edge& e, bool directed = false) {
    edges.push_back(e);
    adj[e.src].emplace_back(m, e.dst);
    if (not directed) adj[e.dst].emplace_back(m, e.src);
    return m++;
  }
};

struct dfs_forest : graph {
  using T = decltype(edge::cost);
  vector<int> root, pv, pe, sz, dep, min_dep, last, ord, in, out;
  vector<T> dist;
  int trials;
  dfs_forest(int _n = 0) : graph(_n), dist(n), trials(0) {
    for (auto p : {&root, &pv, &pe, &sz, &dep, &min_dep, &last, &in, &out})
      p->assign(n, -1);
  }
  int add(const edge& e) { return graph::add(e); }
  void dfs(int v) {
    sz[v] = 1, min_dep[v] = dep[v], last[v] = trials;
    in[v] = size(ord), ord.push_back(v);
    for (auto [id, u] : adj[v]) {
      if (id == pe[v]) continue;
      if (last[u] == trials) {
        min_dep[v] = min(min_dep[v], dep[u]);
        continue;
      }
      root[u] = root[v], pv[u] = v, pe[u] = id, dep[u] = dep[v] + 1;
      dist[u] = dist[v] + edges[id].cost;
      dfs(u);
      sz[v] += sz[u], min_dep[v] = min(min_dep[v], min_dep[u]);
    }
    out[v] = size(ord);
  }
  void build(int r, bool clear_ord = true) {
    root[r] = r, pv[r] = pe[r] = -1, dep[r] = 0, dist[r] = T{};
    if (clear_ord) ord.clear();
    dfs(r);
    ++trials;
  }
  void build() {
    fill(begin(root), end(root), -1);
    for (int v = 0; v < n; ++v)
      if (root[v] == -1) build(v, v == 0);
  }
  int farther(int id) const {
    int u = edges[id].src, v = edges[id].dst;
    return dep[u] < dep[v] ? v : u;
  }
  bool spans(int id) const { return id == pe[farther(id)]; }
  bool anc(int u, int v) const { return in[u] <= in[v] and out[v] <= out[u]; }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m, base;
  cin >> n >> m >> base;
  dsu d(n);
  dfs_forest g(n);
  while (m--) {
    int u, v, z;
    cin >> u >> v >> z;
    --u, --v;
    if (d.same(u, v)) continue;
    d.unite(u, v);
    g.add({u, v, z});
  }
  g.build();

  using mint = modular<power(10, 9) + 7>;
  mint res;
  for (int v = 1; v < n; ++v) {
    res += power<mint>(base, g.edges[g.pe[v]].cost) * g.sz[v] * (n - g.sz[v]);
  }
  cout << res.v << '\n';
}
0